我是xcode的新手,还在学习它的基础知识 . 如果我的问题感觉太基础,请耐心等待 .
我有一个观点,这是我的主要观点 . 当用户单击按钮(选项按钮)时,我想转到第二个视图 . 在这里,用户可以从许多可用中选择一个选项
一旦用户选择了他的选项,我想将此值发送回第一个视图并从那里恢复处理(当用户单击选项按钮时) .
重要 - 用户将在处理过程中从view1移动到view2 . 我必须从view2返回到view1并从我离开的地方继续...这意味着,我需要所有变量值都可用 .
push segue不会创建父视图控制器的新版本 . 您是在viewDidLoad还是在viewDidAppear / viewWillAppear中启动会话处理?你是否在viewWillDisappear中停止处理?在viewWillDisappear中,您可以使用以下代码片段检查导航控制器的堆栈,以查看您的父视图是否已被覆盖或从堆栈中弹出:
- (void)viewWillDisappear:(BOOL)animated { if ([[[self navigationController] viewControllers] indexOfObject:self] == NSNotFound) { // we are disappearing. Clean up the session. ... } [super viewWillDisappear:animated]; }
您可以使用UIViewController的 encodeRestorableStateWithCoder: 方法在视图控制器中存储(有限数量)状态 - 请查看文档 . 我自己不使用它,但它可以帮助 .
顺便说一下,您可能会发现使用UIViewController的 done :, reset: 和 canPerformUnwindSegueAction:fromViewController:withSender: 更容易,而不是创建委托协议 . Unwind segues是在iOS 6中引入的,代码需要的代码少于代理 .
简单!您所要做的就是控制从UIButton到新视图的拖动 . 选择模态 .
听起来你需要实现一个委托 . 我不是最好的解释协议和代表,所以我会提供谈论它们的链接 . 我将在下面提供一个粗略的实现 .
协议https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html
代表们https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html#//apple_ref/doc/uid/TP40010810-CH11
两者之间的区别Difference between protocol and delegates?
View2.h
@protocol View2; @interface View2 : UIViewController @property (nonatomic, weak) id<View2Delegate> *delegate; ... @end @protocol View2Delegate -(void)view2Finished:(NSString *)value; @end
View2.m
@synthesize delegate -(void)backButtonPressed { [delegate view2Finished:@"Value to be passed to view 1"]; }
View1.h
// This code says that View1 implements View2Delegate @interface View1 : UIViewController<View2Delegate>
View1.m
-(void)view2Finished:(NSString *)value { NSLog(@"Value received from view 2: %@", value); }
3 回答
push segue不会创建父视图控制器的新版本 . 您是在viewDidLoad还是在viewDidAppear / viewWillAppear中启动会话处理?你是否在viewWillDisappear中停止处理?在viewWillDisappear中,您可以使用以下代码片段检查导航控制器的堆栈,以查看您的父视图是否已被覆盖或从堆栈中弹出:
您可以使用UIViewController的 encodeRestorableStateWithCoder: 方法在视图控制器中存储(有限数量)状态 - 请查看文档 . 我自己不使用它,但它可以帮助 .
顺便说一下,您可能会发现使用UIViewController的 done :, reset: 和 canPerformUnwindSegueAction:fromViewController:withSender: 更容易,而不是创建委托协议 . Unwind segues是在iOS 6中引入的,代码需要的代码少于代理 .
简单!您所要做的就是控制从UIButton到新视图的拖动 . 选择模态 .
听起来你需要实现一个委托 . 我不是最好的解释协议和代表,所以我会提供谈论它们的链接 . 我将在下面提供一个粗略的实现 .
协议https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html
代表们https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html#//apple_ref/doc/uid/TP40010810-CH11
两者之间的区别Difference between protocol and delegates?
View2.h
View2.m
View1.h
View1.m