我在一个单独的视图中有很多行的UIPickerView,settingsViewController。选择行时,它会通过NSUserDefaults将数据传递给mainViewController。但是,如果我返回settingsViewController并且在不更改行的情况下按下保存按钮 - pickerView似乎发送值'nil'?
这是UIPickerView的代码和传递行的代码。
//PickerViewCode
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[picker selectRow:[defaults integerForKey:@"pickerRow"] inComponent:0 animated:YES];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:
(NSInteger)component {
{
if ([skolor count] == 0)
return 1;
return [skolor count];
}}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
return [skolor objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:row forKey:@"pickerRow"];
//Passing pickerViewValue
- (IBAction)selectedRow:(id)sender {
SchoolSoftViewController2 *vc1 = [self.storyboard
instantiateViewControllerWithIdentifier:@"webview1"];
NSString *urlString = [valdSkola absoluteString];
[[NSUserDefaults standardUserDefaults] setObject:urlString forKey:@"UserURL"];
[self presentViewController:vc1 animated:YES completion:nil];
}
答案 0 :(得分:0)
NSUserDefaults在使用setObject:forKey:方法调用设置后不会立即保存信息。要强制它保存,请在默认情况下设置urlString之后,但在呈现视图控制器之前告诉它进行同步。
NSString *urlString = [valdSkola absoluteString];
[[NSUserDefaults standardUserDefaults] setObject:urlString forKey:@"UserURL"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self presentViewController:vc1 animated:YES completion:nil];
还有你设置行的地方:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:row forKey:@"pickerRow"];
[defaults synchronize];
答案 1 :(得分:0)
尝试设置选择器选择值。就像这样。
int row = [pickerView selectedRowInComponent:COMPONANT_INDEX]; NSString * str = [YOUR_ARRAY objectAtIndex:row]; //如果数组为空,它可能会崩溃
[[NSUserDefaults standardUserDefaults] setObject:str forKey:@“UserURL”]; [[NSUserDefaults standardUserDefaults] synchronize];
在你的情况下, NSString * urlString = [valdSkola absoluteString]; //这将返回一个零