Problem with Modal View Controller

Discussion in 'iOS Programming' started by skunkio, Apr 25, 2010.

  1. skunkio macrumors newbie

    Joined:
    Apr 25, 2010
    #1
    Hi all,
    i'm a new user in this forum and i'm trying to learn how to develop an iphone app Following my book there is an example to use a modal view but something is not working
    Whitin my implementian class i wrote:

    Code:
    - (void)edit
    {
        self.editingViewController.movie = self.movie;
        [self presentModalViewController:self.editingViewController animated:YES];
        NSLog(@"richiamato metodo edit");
    }
    My app should set editingViewController.movie as self.movie and then a view should appear showing (maybe) all the movie values.
    My code has ben compiled properly but when i press the button linked to the edit method nothing happens.
    Watching the console i see:

    Code:
    [cut...]
    Current language:  auto; currently objective-c
    2010-04-25 22:22:34.590 Movie[3801:20b] Application tried to present a nil modal view controller on target <MovieViewController: 0x3e21520>.
    2010-04-25 22:22:34.591 Movie[3801:20b] richiamato metodo edit
    and also my note "richiamato metodo edit" has been executed but i don't see the view.
    Any idea about that?! Thanks in advance.

    Ciao,
    stè
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    The error is very clear on what is wrong: you have called presentModalViewController with a nil value for the view controller. So editingViewController is nil. Where have you set this to a valid, non-nil object?
     
  3. skunkio thread starter macrumors newbie

    Joined:
    Apr 25, 2010
    #3
    Yuo are totally right. I modified my code adding in my implementation class, whitin the viewDidLoad method the red tex:

    Code:
    - (void) viewDidLoad {
    	[super viewDidLoad];
    	
    	Movie *newMovie = [[Movie alloc] initWhithTitle:@"Iron Man" boxOfficeGross:@"100" summary:@"smart guy makes cool armor"];
    	self.movie = newMovie;
    	
    	[COLOR="Red"]MovieEditorViewController *editingViewController = [[MovieEditorViewController alloc] init];
    	self.editingViewController = editingViewController;[/COLOR]
    }
    Thanks for your support.

    Ciao,
    stè
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    If your properties are defined as retain you should release the alloc/init created objects after setting the properties...
     
  5. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #5
    This, plus:

    If your parent controller has been loaded from a NIB file, I'd just go and create the instance of your edit controller in the NIB file and wire it up to your editViewController property by making it an IBOutlet, although its a matter of preference. Generally, if I'm using NIBs, I try to keep any objects that can be initialised in the NIB file (like controllers) in there.

    If you aren't using NIB files, then the code posted is fine but don't forget robbieduncan's memory management advice. You may want to read the Apple Memory Management programming guide if you haven't already.

    Also, if the value of self.movie doesn't change, why not just assign it directly to the editingViewController in the first place?
     
  6. skunkio thread starter macrumors newbie

    Joined:
    Apr 25, 2010
    #6
    If you meaning why i wrote this code:

    Code:
    MovieEditorViewController *editingViewController = [[MovieEditorViewController alloc] init];
    	self.editingViewController = editingViewController;
    
    using two variables i see the code initializes properly self.editingViewController.
    I post another example:

    Code:
    //works
    EventViewController *newEventController = [[EventViewController alloc] init];
    self.eventController = newEventController;
    
    //dosn't work
    EventViewController *eventController = [[EventViewController alloc] init];
    
    don't know why...maybe i'm doing something wrong...i hope...

    Ciao,
    stè
     

Share This Page