Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're using ARC, you don't need to worry about releasing the existing view controller; the compiler will do it for you. If you aren't using ARC, but you are using synthesized properties, then the compiler will still handle the memory management for you. If you write your own accessor methods and are not using ARC, just ensure that the your accessor methods obey the memory management rules of accessor methods. If you aren't using your accessor methods, then release the old ivar before overwriting it with the new value.</p> <p>Edited: As far as replacing a view controller, you need to carefully distinguish between the view controller and its view.</p> <p>The view controller doesn't need to be reloaded, although that's fine. But the same object which originally loaded it should also be the object that reloads it. Always think in terms of objects, responsibility, and relationships (object ownership).</p> <p>Usually you do something like this:</p> <pre><code>- (void)resetCurrentViewController { UIViewController *old = self.currentViewController; UIView *oldView = old.view UIView *superview = [oldView superview]; UIViewController *new = [[MyViewController alloc] initWithNibName:nil bundle:nil]; UIView *newView = new.view; [UIView transitionWithView:superview duration:0.2 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{ [oldView removeFromSuperview]; [containerView addSubview:newView]; } completion:^{ self.currentViewController = new; }]; } </code></pre> <p>This is written in the edit box, not from real code, using stand-in variable names. The basics is that "self" (whatever that is) "owns" the "currentViewController", and replaces the current copy with a new copy, while at the same time swapping the old view for the new one, which one assumes will have the new values.</p> <p>But as another commenter said, this seems like overkill. You could alternately reset all the values in the view controller (implement a -reset method in the view controller to do that) and the view (including any text fields or other controls), wrapping the view updates in an animation so you get a nice fade. But there are lots of different ways to do it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COThats reassuring and I'm using ARC with iOS6 but I'm not using storyboards. Btw, I would like to reconfirm this. I'm showing a viewController and in the middle of the quiz, the user can change his mind and start all over. At this point, I would like to animate the controller (setModalTransitionStyle), invalidate the timer and load the controller as if the user is doing it for the first time. I was reading in here where some people were saying it is bad to add code from the currently viewed controller to reload it.
      singulars
    2. COHow you reload a view controller depends on how that controller was added in the first place. If you're in a navigation controller, you will probably have to pop it and push a replacement. An instance method of an object will continue to run even if that instance is freed in the middle of the method, so you absolutely cannot access ivars, but otherwise it's not seriously bad. It might be confusing to code maintainers. Better to move the logic into a parent view controller or the app delegate.
      singulars
    3. COI'm not using navigation controllers at all. just plain view controllers. Btw, in the code above inside the Refresh button, if I add dismiss code to dismiss the current view controller and have the remaining code (as above) run, I get BAD_ACCESS error. If I comment out the dismiss code, all is fine. Since you mentioned that using ARC, the runtime will take care of releasing the loaded uiview controller, I think the code is good.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload