Going back to superview

Discussion in 'iOS Programming' started by Kingbombs, Sep 3, 2009.

  1. Kingbombs macrumors member

    Joined:
    Jun 24, 2009
    #1
    Hey,
    So i have bassicly a main menu, and when the user presses a certain button the buttons on the view are set to hidden and then i load the view up (create an instance of the class which is a UIViewController and then add as a subview)
    now i want a button to get back to main menu
    if i do [self.view removeFromSuperview];
    Then i just get a blank screen
    The problem here is that, i make the button's not visible, so i have a method that makes all the main menu buttons visible again, but i can't find a way to call that method
    i've tried to create an instance of the parent view and then call the method and then remove from the superview but that doesn't work (didnt think it would)

    i tried something like:
    UIView *parent = [self superview];
    if (parent)
    [parent callMethod];
    but that doesn't work either

    If anyone could help me out that would be appreciated!
    btw i did imported the class file so i could call the method from the instance of the class
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Instead of adding as a subview you might want to try presenting a modal view and then dismissing it when you want to go back to the menu. You don't even need to animate the transition if you don't want to.
     
  3. Kingbombs thread starter macrumors member

    Joined:
    Jun 24, 2009
    #3
    thanks for the reply
    i have been reading the apply documentation on it,
    do you know of any good code examples of this being used?
    Everything i found or saw was using table views and things like this, i just want to create a button which can then call dismissModelViewController so i can get back to the root view
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    You should use a navigation controller. If you don't want to see a navbar you can make it hidden. Then it's a simple matter of pushViewController, popViewController.
     
  5. Kingbombs thread starter macrumors member

    Joined:
    Jun 24, 2009
    #5
    i got it working
    apple docs had this which lead me in a wrong direction:
    [[self firstNavigationController] presentModalViewController:secondNavigationController animated:YES];
    just need self not firstNav...
    thanks for the help
     
  6. Kingbombs thread starter macrumors member

    Joined:
    Jun 24, 2009
    #6
    I have a few problems,
    It seems to works when i click a button to load that view, the view has a timer and it enters information in the database,
    then when i click to leave the view i go back to main menu
    now if i click on the same button to load that view again, it crashes
    If i don't get the timer to run then i can go back and fourth between the views, but when the timer is turned on then i cannot go back onto the view after i have been on it once

    I wasn't sure what to do with this or if there is something i should call before dismissModalViewController
    maybe in the viewdidUnload method?

    sometimes the timer is set to [timer invalidate];
    which is what i call when the button is pressed to return to the menu (which means i invalidate it twice sometimes, which causes an error)
    i tried:
    if ([timer isValid])
    [timer invalidate];

    but this seemed to cause an error at run time

    if anyone knows of anything that can help for any of those problems that would be good thank you
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Perhaps have the database updated in a spawned thread?

    What kind of error? More details please!
     
  8. Kingbombs thread starter macrumors member

    Joined:
    Jun 24, 2009
    #8
    So i had it fixed initially, i was releasing the timer in the dealloc method but if i did [timer invalidate]; already, then it would throw an error
    (but if i just have [timer release]; in the dealloc method, it wouldn't stop the timer)
    so release it when i close the view now when the button is pressed

    But if the timer has already been set to invalidate - [timer invalidate];
    then when i try if ([timer isValid])
    it throws an error:
    btw this is timer declared in .h: NSTimer *countdownTimer;
    Code:
    04/09/2009 12:06:08 Baseball[997] *** -[NSCFData isValid]: unrecognized selector sent to instance 0xd4c340 
    ON THE NEXT LINE IT ALSO HAS:
    04/09/2009 12:06:08 Baseball[997] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFData isValid]: unrecognized selector sent to instance 0xd4c340' 
    
    There is also a stack trace i could print but i don't know if it will make any difference (is there a site on how to read stack trace or what it shows)

    So if anyone could help with that, i would have thought if i did [timer invalidate] then when i did [timer isValid]; it would return no and not cause that error

    Also on a side note is there a very quick and easy way to put a background picture on a view? (spend most of my time on opengl so view based and IB stuff im a noob on, and figured i should use it rather than opengl to learn it)
    I just have a picture that is the size of the screen and just want it as the background for all the views

    thanks anyone who can help
     
  9. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #9
    After you invalidate your timer set the ivar to nil. Always, always, always do this.
     
  10. Kingbombs thread starter macrumors member

    Joined:
    Jun 24, 2009
    #10
    Thanks this is working really well!

    any help on the background picture please?
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    From the NSTimer class Reference:

    Setting your ivar to nil prevents your code from referencing an already released object.
     

Share This Page