Push a nil view controller on deployed app
My question is pretty simple: On the iPhone/iPad simulator, when you push a nil view Controller, you are greeted by the gracious message
Application tried to push a nil view controller on target
after which the app continues execution as if 开发者_如何学Cnothing happened. This is exactly the type of behavior I want of the app (I have a factory method, which, if encounters an incorrect request for the creation of an instance, returns nil).
However I have doubts as to whether this will fly as is on the real iOS device. Since I cannot deploy in this early stage of development, I would like to know whether this action would result in a crash, or it will graciously do nothing (as expected). Furthermore, if it works on the iOS, will I get slapped by Apple in the review process for doing 'bad things'?
When it comes to Cocoa Touch methods, you should never pass a nil parameter unless the reference documentation explicitly states that a nil parameter is valid. If the documentation is ambiguous, don't rely on empirical testing (in the simulator or otherwise) to decide that it's safe. Apple may release a future OS update that suddenly causes your app to start crashing for your existing users. Then they'll give your app bad reviews for a week or two while Apple reviews your hotfix. It's just not worth the risk.
The documentation for pushViewController:animated:
doesn't specify how it will behave if given a nil parameter, so play it safe and test for nil in your code.
This means that you are trying to push a view controller which is not allocated yet. Make sure the view controller is not nil before you push it into a navigation controller.
If a nil is pushed into a navigation controller stack, the app just throws the message "Application tried to push a nil view controller on target ...", in the console, and it won't disturb the execution of the app. But it is always good to be on the safer side by checking for nil, to prevent any unexpected behaviour.
精彩评论