Im new to Objective and iPad apps. Im trying to display an array that Ive already used in a separate Class/View Controller. The array Im trying to access is in "MainMenuScreen.m" and I want to use that array in another class called "ScoresScreen.m".
All my code is working, I just need to get the array from the other class and plug into my code.I tried using Segue but im getting this error in my code, and im stuck. Thankyou in advance
.h
#import <UIKit/UIKit.h>
#interface ScoresScreen : UIViewController<UITableViewDataSource, UITableViewDelegate>{
NSMutableArray *scoresArray;
}
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (weak, nonatomic) NSMutableArray *scoresArray; //Added this line
#end
.m
#import "ScoresScreen.h"
#import "MainMenuScreen.h"
#interface ScoresScreen ()
#end
#implementation ScoresScreen
#synthesize scoresArray; //Added this line
NSMutableArray *TestScoresArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
...
....
....
.m
#import "MainMenuScreen.h"
#import "PlayGame.h"
#import "ImageTest.h"
#import "ScoresScreen.h"
#interface MainMenuScreen ()
#end
#implementation MainMenuScreen
//---------------------------------------------------------------------------------
//------------------------------------Variables------------------------------------
PlayGame *playGame;//PlayGameObject
NSMutableArray *gameDataArray;//Game Data Array (A array storying multable ImageTest Objects)
NSMutableArray *scoresArray;//<-------This need to go to view scores
//---------------------------------------------------------------------------------
//----------------------------------Constructors-----------------------------------
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
scoresArray = [[NSMutableArray alloc] init];
[scoresArray addObject: [NSMutableArray arrayWithObjects:#"Bob",[NSNumber numberWithInt:134],nil]];//testData
[scoresArray addObject: [NSMutableArray arrayWithObjects:#"Roger",[NSNumber numberWithInt:12],nil]];//testData
[scoresArray addObject: [NSMutableArray arrayWithObjects:#"Ben",[NSNumber numberWithInt:34],nil]];//testData
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
//---------------------------------------------------------------------------------
//----------------------------------Add New Score----------------------------------
//a setter Method so Play Game can add the user name and score to the scoresArray
-(void)AddNewScore :(NSString *)userName :(int)score
{
//adds user name and score to the array
if (scoresArray != nil)
{
/*NSMutableArray *nameAndScore = [[NSMutableArray alloc] init];
[nameAndScore addObject:userName];
[nameAndScore addObject: [NSNumber numberWithInt:score]];//int needs to be converted before storing in an array
[scoresArray addObject:nameAndScore];*/
[scoresArray addObject: [NSMutableArray arrayWithObjects:userName,[NSNumber numberWithInt:score],nil]];
for(NSMutableArray *m in scoresArray)
{
NSLog(#"%#%#", m[0],m[1]);//prints out the array in logs to make sure it is working
}
}
}
//added this method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
ScoresScreen *controller = [segue destinationViewController];
controller.array = yourArray; //use of undeclared identifier 'yourarray'
//and this error "property 'array' not found on object of type 'ScoresScreen*'"
}
//This is where I get the error ^.
...Please can someone help me? Thanks
---------
Use this in your MainMenuScreen.m file
#import "MainMenuScreen.h"
#import "PlayGame.h"
#import "ImageTest.h"
#import "ScoreScreen.h"
Related
My cpViewController has a button that modal segues to cpScanViewController. I want to have cpScanViewController inform ViewController with a string when it has completed a successful scan. After reading numerous articles online, I believe delegation is the best way to do this?
Is the delegate being set correctly?
How does the delegate inform the cpViewController?
cpScanViewController.h
#import <UIKit/UIKit.h>
#protocol ScanDelegate <NSObject>
-(void)receivedUPC:(NSString *)Scan;
#end
#interface cpScanViewController : UIViewController
#property (nonatomic, weak) id <ScanDelegate> delegate;
#property (nonatomic, retain) NSString *UPC;
-(void)checkUPC;
#end
cpScanViewController.m
#interface cpScanViewController ()
{
NSString *UPC;
}
#end
#implementation cpScanViewController
#synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)doSomeScanStuff
{
UPC = #"a string"
// even tried
[delegate receivedUPC:UPC];
}
-(void)checkUPC
{
}
#end
cpViewController.h
#import <UIKit/UIKit.h>
#import "cpScanViewController.h"
#interface cpViewController : UIViewController <ScanDelegate>
#end
cpViewController.m
#interface cpViewController ()
{
cpScanViewController *cp;
}
#end
#implementation cpViewController
- (void)viewDidLoad
{
// set delegate
cp = [[cpScanViewController alloc] init];
[cp setDelegate:self];
}
-(void)receivedUPC:(NSString *)Scan{
// Nothing happening
NSLog(#"%#", Scan);
NSLog(#"The %#", cp.UPC);
}
#end
Because you are not calling receivedUPC method from your cpScanViewController class. You need to add [_delegate receivedUPC:self], some where in order to invoke this delegat method in cpViewController class.
I was able to get it working by using the method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
cpScanViewController *vd = (cpScanViewController *)[segue destinationViewController];
vd.delegate = self;
}
Because I made the connection in storyboards I didn't realize it needed to have the method.
Question with trying to simply clear a mutablearray in another class in objective C
the code all runs fine, just the data doesnt get wiped....
SecondViewController.h
#interface SecondViewController : UIViewController <UITableViewDelegate,UITextFieldDelegate,ABPeoplePickerNavigationControllerDelegate>
{
AppDelegate *appDelegate;
}
#property (nonatomic, retain) NSMutableArray *contactsArray;
Thats the declaration of the mutablearray...
then from the class that i am trying to delete it.
listViewController.h
#import "SecondViewController.h"
#interface ListViewController : UIViewController <UITableViewDelegate,ABPeoplePickerNavigationControllerDelegate,UITabBarControllerDelegate>{
AppDelegate *appDelegate;
SecondViewController *secondViewControl;
}
#property (nonatomic, retain) IBOutlet UITableView *table;
then in the main file i use the code
[secondViewControl.contactsArray removeAllObjects];
but then when i check the array it has not been emptied...
EDIT:
#implementation ListViewController
#synthesize table;
/*- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}*/
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
secondViewControl = [[SecondViewController alloc]init];
I'm just trying to understand how delegate works and I'm in troubles.
I have two classes (both UIViewController) connected into the storyboard, the first one (ViewController.h/m) hold a TableView with cells and the second one (AddNameViewController.h/m) simply hold a TextField (where I want to write) and a button (Add Name)
as you surely understand I want the button pressed to send to the TableView what is written into the TextField, pretty simple.
And since I have two different Controllers and an Array containing the data holds by the tableview, I want to connect them with a delegate (just to learn it).
here is some code:
ViewController.h
#import "AddNameViewController.h"
#interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, AddNameViewControllerDelegate>
#property (strong, nonatomic) NSMutableArray *array;
#end
ViewController.m
#import "ViewController.h"
#import "AddNameViewController.h"
#inferface ViewController ()
#end
#implementation ViewController
#synthesize array;
-(void)addStringWithString:(NSString*)string
{
[self.array addObject:string];
NSLog(#"%#", array);
}
-(void)viewDidLoad
{
AddNameViewController *anvc = [[AddNameViewController alloc] init];
anvc.delegate = self;
array = [[NSMutableArray alloc] initWithObjects:#"first", #"second", nil];
NSLog(#"%#", array);
[super viewDidLoad];
}
-(NSInteger)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSindexPath*)indexPath
{
static NSString *simpleTableIdentifier = #"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
#end
AddNameViewController.h
#protocol AddNameViewControllerDelegate <NSObject>
-(void)addStringWithString:(NSString*)string;
#end
#interface AddNameViewController : UIViewController
#property (weak, nonatomic) id <AddNameViewControllerDelegate> delegate;
#property (weak, nonatomic) IBOutlet UITextField *myTextField;
-(IBAction)add:(id)sender;
#end
finally the AddNameViewController.m
#import "ViewController.h"
#interface AddNameViewController ()
#end
#implementation AddNameViewController
#synthesize myTextField, delegate;
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
}
-(IBAction)add:(id)sender
{
[self.delegate addStringWithString:self.myTextField.text];
// I've also tried with this but nothing --> [self.delegate addStringWithString:#"aa"];
}
#end
The array is initialized properly, no errors, no warnings, no crashes, simply seems like the method "addStringWithString" is not even called, because is not even NSLog anything.
obviously everything in connected in the storyboard, methods and outlets, thanks for your help.
in interface builder of AddNameViewController, did you connect the button event (Touch Up inside) into the action -(IBAction)add:(id)sender ?
also try this
-(IBAction)add:(id)sender
{
if([self.delegate respondsToSelector:#selector(addStringWithString:)]) {
[self.delegate addStringWithString:self.myTextField.text];
}
// I've also tried with this but nothing --> [self.delegate addStringWithString:#"aa"];
}
I'm trying to implement saving of user preferences in my OS X app. I have an NSTableView which stores its data to my .plist file, but I have troubles loading same data back to my NSTableView.
My model object:
#import <Foundation/Foundation.h>
#interface UserKeys : NSObject <NSCoding>
#property (nonatomic, copy) NSString *userKeyName;
#property (nonatomic, copy) NSString *userApi;
#property (nonatomic, copy) NSString *userCode;
#end
and its implementation:
#import "UserKeys.h"
#implementation UserKeys
- (id)init
{
self = [super init];
if (self) {
//
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self.userKeyName = [aDecoder decodeObjectForKey:#"userKeyName"];
self.userApi = [aDecoder decodeObjectForKey:#"userApi"];
self.userCode = [aDecoder decodeObjectForKey:#"userCode"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)enCoder {
[enCoder encodeObject:self.userKeyName forKey:#"userKeyName"];
[enCoder encodeObject:self.userApi forKey:#"userApi"];
[enCoder encodeObject:self.userCode forKey:#"userCode"];
}
#end
My preferences view controller header:
#import <Cocoa/Cocoa.h>
#import "MASPreferencesViewController.h"
#import "UserKeys.h"
#interface PreferencesApiKeysViewController : NSViewController <MASPreferencesViewController>
#property (strong) NSMutableArray *allKeys;
#end
and its implementation:
#import "PreferencesApiKeysViewController.h"
#import "UserKeys.h"
#interface PreferencesApiKeysViewController ()
#property (weak) IBOutlet NSTableView *keysTableView;
#end
#implementation PreferencesApiKeysViewController
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle {
if ((self = [super initWithNibName:nibName bundle:bundle])) {
self.allKeys = [NSMutableArray array];
}
return self;
}
// delegating....
//
- (void)viewWillAppear {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:#"UserKeys"];
if (data != nil) {
NSArray *oldArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
if (oldArray != nil) {
self.allKeys = [[NSMutableArray alloc] initWithArray:oldArray];
} else {
self.allKeys = [NSMutableArray array];
}
}
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
// some code...
return cellView;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [self.allKeys count];
}
The error I'm getting is:
-[UserKeys count]: unrecognized selector sent to instance 0x100534df0
When I put a breakpoint in - (void)viewWillAppear, I can see that data has around 250 bytes, but this oldArray seems to be empty. I understand that I'm doing some major mistake here with self.allKeys so that my table is not aware how many rows it should create, but after lots of attempts to get proper data, I'm really out of ideas why is this happening.
So, how to properly unarchive my data and present it in NSTableView?
(I'm using Xcode 4.6)
It is because the object you appear to be dearchiving is a pointer to an object of class UserKeys, rather than an object that is or descends from NSArray. When -initWithAray: hits your oldArray pointer to UserKeys, it doesn't know, or doesn't care what class it is, it simply sends it a -count message so as to ascertain the size it must grow to.
I am trying to create an app and when I added this IBAction, it gave me the error Missing context for method declaration. I have looked up how to fix this but I am a bit confused since I am a beginner. The errors appear in my .m file:
#import "QRscannerSecondViewController.h"
#end
#interface QRscannerSecondViewController ()
#end
#implementation QRscannerSecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Info", #"Info");
self.tabBarItem.image = [UIImage imageNamed:#"second"];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
-(IBAction)button1
{
[input resignFirstResponder];
l11 = input.text;
line11.text = [[NSString alloc] initWithFormat:#"%2.f", l11];
}
but I though that it would help if I included my .h file, also.
#import <UIKit/UIKit.h>
#interface QRscannerSecondViewController : UIViewController
#end
#interface TextGameViewController : UIViewController
{
IBOutlet UITextField *input;
IBOutlet UILabel *line11;
NSString *l11;
}
-(IBAction)button1;
You need to put the code:
-(IBAction)button1
{
[input resignFirstResponder];
l11 = input.text;
line11.text = [[NSString alloc] initWithFormat:#"%2.f", l11];
}
before the #end.