2012-05-01 27 views
0

私はルートと2番目のビューを持つナビゲーションコントローラを持っています。 2番目のビューには、ユーザーが値0〜9を設定できるpickerviewがあります。この値をnsuserdefaults に保存し、rootvievのtableviewのnumberofrowsinsectionにこの値を使用します。 私はpickervalueフォームuserdefaultsを取得し、それをNSIntegerに設定します。 NSLogは正しい値を表示しますが、テーブルの戻り値として - >行はありません。テーブルビューが動作し、4の戻り値は私に4行を示します...テーブルビューに行が表示されない

私のためのアイデア?ここ

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

と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 
+0

thx!完璧に動作します! – HugoBoss

+0

誰かがピッカーを使用したことをテーブルにどのように伝えていますか?私は 'pickerView:didSelectRow:'に何かがあると予想して、テーブルのリロードを通知します。 –

+0

別の質問:2番目のビューから1番目のビューに切り替えた後、テーブルビューをリロードする簡単な方法はありますか? – HugoBoss

答えて

0

ルートビューでviewWillAppear代わりのviewDidAppearにあなたの番号をロードするためのコードを移動します。 (テーブルビューが読み込まれて表示された後で)番号を読み込み中です。

関連する問題