问题
I just wanted to implement the iOS State-Restoration APIs in one of my Apps. After finally getting it to work, I discovered that a ViewController that I present modally gets restored using an animation, which is not what I want. I would expect my app to just be in the state I left it, but not having the user to see hot he got there.
So I went ahead and downloaded Apple Sample Code on that: https://developer.apple.com/library/ios/samplecode/StateRestore/Introduction/Intro.html and wanted to see if it happens there as well. And it turns out that it does.
Further there is a warning in the Logs:
Unbalanced calls to begin/end appearance transitions for <UINavigationController: 0x7b0512b0>.
Can you tell me if I and obviously Apples Sample Code are doing something wrong, or if it's a bug in iOS?
Btw. I was testing on iOS8
Thanks for any help, Georg
回答1:
The following solution comes directly from Apple.
In your app delegate, you should be implementing application:willFinishLaunchingWithOptions:
(instead of or in addition to didFinishLaunching
). In your implementation, probably as the last line just before returning true
(or YES if this is Objective-C), insert this line:
self.window?.makeKeyAndVisible()
Or, if this is Objective-C:
[self.window makeKeyAndVisible];
It turns out that this was always needed, but the documentation has never been clear about it.
回答2:
From documentation: Preserving Your App’s Visual Appearance Across Launches
See the 3rd item from the checklist below.
Checklist for Implementing State Preservation and Restoration
Supporting state preservation and restoration requires modifying your app delegate and view controller objects to encode and decode the state information. If your app has any custom views that also have preservable state information, you need to modify those objects too.
When adding state preservation and restoration to your code, use the following list to remind you of the code you need to write.
- (Required) Implement the application:shouldSaveApplicationState: and application:shouldRestoreApplicationState: methods in your app delegate; see Enabling State Preservation and Restoration in Your App.
(Required) Assign restoration identifiers to each view controller you want to preserve by assigning a non empty string to their
restorationIdentifier property; see Marking Your View Controllers for Preservation.If you want to save the state of specific views too, assign non empty strings to their restorationIdentifier properties; see Preserving the State of Your Views.
(Required) Show your app’s window from the application:willFinishLaunchingWithOptions: method of your app delegate. The state restoration machinery needs the window so that it can restore scroll positions and other relevant bits of your app’s interface.
Assign restoration classes to the appropriate view controllers. (If
you do not do this, your app delegate is asked to provide the
corresponding view controller at restore time.) See Restoring Your
View Controllers at Launch Time.- (Recommended) Encode and decode the state of your views and view controllers using the encodeRestorableStateWithCoder: and decodeRestorableStateWithCoder: methods of those objects; see Encoding and Decoding Your View Controller’s State.
Encode and decode any version information or additional state information for your app using the application:willEncodeRestorableStateWithCoder: and application:didDecodeRestorableStateWithCoder: methods of your app delegate; see Preserving Your App’s High-Level State.
Objects that act as data sources for table views and collection views should implement the UIDataSourceModelAssociation protocol. Although not required, this protocol helps preserve the selected and visible items in those types of views. See Implementing Preservation-Friendly Data Sources.
回答3:
Apple sample code seems to work fine on Xcode 8.
So I suppose no additional code changes would be required
来源:https://stackoverflow.com/questions/26461689/ios-state-restoration-animation-bug