Note that there are some explanatory texts on larger screens.

plurals
  1. POretain array data outside of method objective-c
    primarykey
    data
    text
    <p><strong>I have an array, <code>players</code>, with two strings inside it: <code>player1</code> and <code>player2</code>. Here is my .h file:</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @interface hardOne : UIViewController { UISwitch *hard1ON; NSMutableArray *players; NSString *player1, *player2; } @property (nonatomic, retain) IBOutlet UISwitch *hard1ON; @property (nonatomic) BOOL switchState; @property (nonatomic, retain) NSMutableArray *players; - (IBAction) switchValueChanged; @end </code></pre> <p><strong>The array is initialized in the viewDidLoad then the data is entered into that array in two IBActions in my .m file:</strong></p> <pre><code>#import "hardOne.h" @interface hardOne () &lt;UITextFieldDelegate&gt; @property (nonatomic, strong) IBOutlet UITextField *textFieldOne; @property (nonatomic, strong) IBOutlet UITextField *textFieldTwo; @end @implementation hardOne @synthesize hard1ON; @synthesize players; @synthesize textFieldOne; @synthesize textFieldTwo; BOOL switchState; int counter = 0; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; [hard1ON setOn:switchState animated:NO]; //read player names to user defaults [textFieldOne setText:[[NSUserDefaults standardUserDefaults] stringForKey:@"player1"]]; [textFieldTwo setText:[[NSUserDefaults standardUserDefaults] stringForKey:@"player2"]]; self.players = [[NSMutableArray alloc] init]; NSLog(@"%@",self.players); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction) switchValueChanged { counter += 1; if (counter % 2 == 0) { switchState = 0; } else { switchState = 1; } if (hard1ON.on) { [[NSNotificationCenter defaultCenter] postNotificationName:@"theChange" object:nil]; } else { [[NSNotificationCenter defaultCenter] postNotificationName:@"theChange2" object:nil]; } } - (IBAction) returnKey1 { player1 = [textFieldOne text]; [self.players addObject:(player1)]; //set player1's name to user defaults [[NSUserDefaults standardUserDefaults] setValue:[textFieldOne text] forKey:@"player1"]; } - (IBAction) returnKey2 { player2 = [textFieldTwo text]; [self.players addObject:(player2)]; //set player2's name to user defaults [[NSUserDefaults standardUserDefaults] setValue:[textFieldTwo text] forKey:@"player2"]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return NO; } @end </code></pre> <p>If I use <code>NSLog</code> in the second IBAction, once it is complete, the array is correctly displayed in the console with the strings <code>player1</code> and <code>player2</code>, however if I try to use the array anywhere else it is <code>null</code>. Could anyone point me in the right direction?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload