i have a navigationcontroller with the root and a second view. the second view contains a pickerview where the user can set the value 0 to 9. i save this value in the nsuserdefaults
and will use it for the numberofrowsinsection in the tableview on the rootviev.
i retrieve the pickervalue form the userdefaults and set it to a NSInteger. NSLog shows me the correct value, but as return value for the table -> no rows. the tableview works, a return value of (for example) 4 shows me 4 rows…
any ideas for me?
here the rootview:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return retValv ;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
cell = [UITableViewCell alloc];
cell = [cell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.text = @"Test";
return cell;
}
-(IBAction)switch1:(id)sender {
secondView *second=[[secondView alloc]initWithNibName:@"secondView" bundle:nil];
[self.navigationController pushViewController:second animated:YES];
}
-(IBAction)showLog:(id)sender
{
NSLog(@"retValv is dzt. %i",retValv);
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
retValv = [defaults integerForKey:@"myInt"];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"Locations";
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
and the secondview:
#import "secondView.h"
@interface secondView ()
@end
@implementation secondView
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent: (NSInteger)component
{
return 10;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:@"%i",row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
NSLog(@"Gewählt: Zeile %i",row);
zeile = row;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:row forKey:@"pickerRow"];
[defaults setInteger:zeile forKey:@"myInt"];
[defaults synchronize];
}
-(IBAction)showInt:(id)sender {
NSLog(@"show value %i",zeile);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = @"Settings";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[picker selectRow:[defaults integerForKey:@"pickerRow"] inComponent:0 animated:NO];
zeile = [defaults integerForKey:@"myInt"];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
In the root view move the code to load your number to viewWillAppear instead of viewDidAppear. It is loading the number too late (after the tableview is loaded and displayed).