Hello and a problem with ViewController

Nibbie
Posts: 4
Joined: 2010.04
Post: #1
Maybe it's rude to register and ask for help immediately, but my brother advised me to use this site because the help is excellent and people are very knowledgeable.

I am starting to get my head around coding in xcode, I have an issue though, one with trying to call methods in a ViewController above the one I'm in. I need to work out a way to do it well.

So, I have 3 view controllers. 1 to control if the game should be played or if the menu should be shown, the menu will end up being more than one view which is why I haven't just made the opengl es view run below the menu with opacity.

Anyway, so I have SwitchViewController and this manages GameViewController and MenuViewController which each have their own views. Now, I have a method in SwitchViewController called ... switchViews that animates a transition between the currently selected XIB to the other XIB (GameView or MenuView).

When I was developing, I put a button in the SwitchViewController and made it so when you press it, it called switchViews. Worked perfectly fine.

Now I have progressed and want to call the switchViews function inside SwitchViewController from the MenuViewController but I can't get it to work.

I'm doing what I think is right in the MenuViewController.h

Code:
#import <UIKit/UIKit.h>

@class SwitchViewController;

@interface GameViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    NSArray *listData;
    SwitchViewController *switchViewController;
}

@property (nonatomic, retain) NSArray *listData;
@property (nonatomic, retain) SwitchViewController *switchViewController;

@end

And in the MenuViewController.m file

Code:
#import "GameViewController.h"
#import "SwitchViewController.h"

@implementation GameViewController
@synthesize switchViewController;
@synthesize listData;

[...]

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // This is called when a tableView item is pressed
    [switchViewController switchViews];
}

The SwitchViewController just has a function defined in the header and it looks like this in the m:

Code:
- (void)switchViews
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Chosen One!" message:@"Something worked" delegate:nil cancelButtonTitle:@"Good show" otherButtonTitles:nil];
    [alert show];
    [alert release];
}
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #2
Sounds to me like maybe you forgot to set switchViewController. Two suggestions that come to mind: You can either make it an IBOutlet and wire it up in your nib, or you could set it programmatically after everything is loaded.

Quote:Maybe it's rude to register and ask for help immediately...
Most definitely not rude at all! That's what we're here for Wink
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2010.04
Post: #3
Isn't that what I'm doing in the MenuViewController.h?

I'm confused. Where do I need to set it? Something to do with interface builder?
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2010.04
Post: #4
Quote:I'm doing what I think is right in the MenuViewController.h

Code:
#import <UIKit/UIKit.h>

@class SwitchViewController;

@interface GameViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    NSArray *listData;
    SwitchViewController *switchViewController;
}

@property (nonatomic, retain) NSArray *listData;
@property (nonatomic, retain) SwitchViewController *switchViewController;

@end

I thought I was setting up the link here? Could you direct me to a place that demos what you're talking about, right now I'm not sure I understand.
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #5
Code:
@property (nonatomic, retain) SwitchViewController *switchViewController;

This just gets Xcode to generate getters and setters (accessors), but it doesn't actually set the switchViewController variable to anything.

I'll bet if you check in the debugger, or do an NSLog of it, right at [switchViewController switchViews], you'll probably see that switchViewController is NULL.

Yes, in InterfaceBuilder, you'd probably instantiate a NSViewController, then set its class to your SwitchViewController, then wire to that from GameViewController. I can't think of any demos to link to at the moment, although the general process is pretty well explained in their basic tutorial at Apple. Sorry I can't remember what it's called since it's been a while.

[edit] Ah, here it is:

https://developer.apple.com/mac/library/...TP40000863

It looks like maybe the section "Bridging the Model and View: The Controller" might have what you're looking for.
Quote this message in a reply
Nibbie
Posts: 4
Joined: 2010.04
Post: #6
Thanks man. I got it sorted by building my entire project from the ground-up based on a window application and I learnt how to do it inside out so I could better understand the MVC stuff in XCODE.

Now I have it sorted, I'm using the following code to talk to other classes:

Code:
#import "TwoViewAppDelegate.h" // import the header of the class you want to reference


- (IBAction)setRedColor:(id)sender
{
    // Use the class name and create a pointer (in this case mainDelegate)
    TwoViewAppDelegate *mainDelegate = (TwoViewAppDelegate *)[[UIApplication sharedApplication] delegate];
    mainDelegate.textColor = [UIColor redColor];
    [mainDelegate flipToFront];
}
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  ViewController vs. GLView Madrayken 3 3,140 Aug 7, 2009 03:41 AM
Last Post: maximile