0

我有一个带有根目录和第二个视图的导航控制器。第二个视图包含一个pickerview,用户可以将值设置为0到9。我将此值保存在nsuserdefaults中,并将其用于rootviev上tableview中的numberofrowsinsection。我从 userdefaults 中检索 pickervalue 并将其设置为 NSInteger。NSLog 向我显示了正确的值,但作为表的返回值-> 没有行。表格视图有效,(例如)4的返回值向我显示4行......

对我有什么想法吗?

这里是根视图:

#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);
}

和第二个视图:

#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
4

1 回答 1

0

在根视图中移动代码以将您的号码加载到 viewWillAppear 而不是 viewDidAppear。它加载数字太晚了(在加载和显示表格视图之后)。

于 2012-05-01T14:16:59.040 回答