[英]Re-opening iOS app through Url Scheme - State Preservation
I'm sending the user from my app to a browser then returning them to the app through my custom URL. 我将用户从我的应用程序发送到浏览器,然后通过我的自定义URL将其返回到应用程序。 The problem is: All of my instance variables are empty once I return!
问题是:一旦我返回,我的所有实例变量都是空的! The interface is preserved (as expected since I used the Storyboard), but the values (of the textfields, instance variables, etc.) are inaccessible...they return null.
接口被保留(正如我使用Storyboard时预期的那样),但是(文本字段,实例变量等)的值是不可访问的...它们返回null。
What's going on?? 这是怎么回事??
(It's been a while since I had this problem, but here's what I think happened…) (自从我遇到这个问题已经有一段时间了,但这就是我认为发生的事情......)
While handling the return URL's call in my app delegate's openURL method, I attempted to call my view controller's method in response and did so by creating an instance of that method's view controller. 在我的app delegate的openURL方法中处理返回URL的调用时,我尝试在响应中调用我的视图控制器的方法,并通过创建该方法的视图控制器的实例来实现。 Since I didn't access the current instance, I essentially created a blank, second instance of that view controller.
由于我没有访问当前实例,因此我基本上创建了该视图控制器的空白第二个实例。
In this case, I ended up using an NSNotification
instead of directly calling the method. 在这种情况下,我最终使用
NSNotification
而不是直接调用该方法。 And I definitely think this is the most elegant solution. 我绝对认为这是最优雅的解决方案。
But what I also could have done in retrospect, is access the current instance of the view controller which also happened to be the root view controller using: 但回想起来我也可以做的是访问视图控制器的当前实例,它也恰好是根视图控制器,使用:
UIViewController *controller = self.window.rootViewController;
The response from the calls in the app delegate should preferably not call for any specific view controller, one of the following should be preferably used to modify data- 来自app委托中的调用的响应最好不要求任何特定的视图控制器,以下之一应优先用于修改数据 -
1.Blocks 1.Blocks
2.NSUserDefaults or CoreData Database 2.NSUserDefaults或CoreData数据库
3.Notifications 3.Notifications
4.Shared Singleton instances 4.Shared Singleton实例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.