iphone - 我们是否总是需要解雇我们提供的ModelViewController?

标签 iphone objective-c nsuserdefaults presentmodalviewcontroller dismiss

我已编辑整个问题

在以下情况下呈现 ModelView Controller 和关闭模态视图 Controller 有什么区别?

  1. 主页 View
  2. TableItemSelection View
  3. 选择器 View

主页 View 中 我正在 viewWillAppear 中编写代码,以从 TableView 设置的 NSUserDefault 中获取值。下面是我的 viewDidLoad (初始值)和 viewWillAppear (当来自 tableview 的新值时)的代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    actionMinutes = @"0";
    actionSeconds = @"0";
    restMinutes = @"0";
    restSeconds = @"0";
}

- (void) viewWillAppear:(BOOL)animated
{
    actionMin = [actionMinutes intValue];
    actionSec = [actionSeconds intValue];
    restMin = [restMinutes intValue];
    restSec = [restSeconds intValue];
    NSLog(@"acMin:%d acSec:%d reMin:%d reSec:%d",actionMin,actionSec,restMin,restSec);
}

TableItemSelection View 中 我从 HomeView 展示这个 View 。现在我想根据 Table 的 didSelectRowAtIndex 方法在 HomeView 中设置 NSString 的值。我正在使用 NSUserDefault 来设置该值。 通过触摸完成按钮,我将呈现 HomeView。 (实际上我必须解雇ModalViewController)但是当我使用解雇时我无法获取HomeView的NSString中的值。我从 PickerView 获取表的值。 (我被指示这样做)。下面是我的“完成”按钮触摸上的“表格 View ”代码

HomeView *homeView = [[HomeView alloc] init];
[homeView.view setBackgroundColor:[UIColor clearColor]];
[homeView.view setFrame:CGRectMake(0 ,40, 320, 460)];
homeView.actionMinutes = [[NSUserDefaults standardUserDefaults] valueForKey:@"actionMinute"];
homeView.actionSeconds = [[NSUserDefaults standardUserDefaults] valueForKey:@"actionSecond"];
homeView.restMinutes = [[NSUserDefaults standardUserDefaults] valueForKey:@"restMinute"];
homeView.restSeconds = [[NSUserDefaults standardUserDefaults] valueForKey:@"restSecond"];
homeView.song = [[NSUserDefaults standardUserDefaults] valueForKey:@"songstring"];
NSLog(@"%@",homeView.actionMinutes);
[self presentModalViewController:homeView animated:YES];

//[self dismissModalViewControllerAnimated:YES];    // if this method is used then no values are passed to HomeView

[homeView release];

PickerView中 我从 pickerview 获取值,然后将其存储在 UserDefault 中。 下面是我的 pickerview 代码

NSUserDefaults *actionTime = [NSUserDefaults standardUserDefaults];
[actionTime setValue:min forKey:@"actionMinute"];
[actionTime setValue:sec forKey:@"actionSecond"];

那么为什么我在关闭 ModelView 时无法获取 UserDefault 值。???每次呈现一个新 View 都会产生一堆 View 吗???

最佳答案

@DShah:是的,绝对是..!!

您需要关闭每个模态视图。

另外请发布您正在使用的代码。

还要记住,您需要首先放置 NSUserDefaults 行(您使用将 NSString 值分配给 NSUserDefaults 的行),然后将该行放在 dismissModalViewControllerAnimated: 中。

如果您需要更多帮助,请给我留言。

希望这对您有帮助。

关于iphone - 我们是否总是需要解雇我们提供的ModelViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7273122/

相关文章:

ios - 如何使用 xamarin visual studio 2015 在 windows 中模拟 ios 应用程序

iphone - 使用核心数据iOS的登录ID和密码

cocoa - 将 SecIdentityRef 存储到 NSUserDefaults 中

ios - NSUserdefaults - 如何在 Swift 2.2 Xcode7 中使用 objectforkey

IOS 和 Objective C - 如何在 Tableview 的 NSUserDefaults 中显示 NSDictionaries 的 NSMutableArray?

ios - 设备上的 PureLayout : unrecognized selector sent to instance

ios - 获取点击手势识别器的标签或索引

ios - 使用异步 NSURLConnection 获取警告

ios - numberOfSections 被调用? iOS

iphone - 静态库中的 Objective-C 类别