ルートと 2 番目のビューを持つナビゲーション コントローラーがあります。2 番目のビューには、ユーザーが値 0 から 9 を設定できるピッカービューが含まれています。この値を nsuserdefaults に保存し、rootviev のテーブルビューの行数セクションに使用します。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