Cristian
Cristian

Reputation: 7175

Calling Method from Other Class Does Nothing?

I have one class called MainMenu, which has a method called switchViews, with 2 parameters, an NSWindow and an NSView.

I then have the AppDelegate, which i want to use this method in the 'applicationDidFinishLaunching'.

So in the AppDelegate.h i have imported the MainMenu.h and in the AppDelegate.m I use as follows:

MainMenu *theMainMenu = [[MainMenu alloc]] init];
[theMainMenu switchViews:param1:param2;

There are no warnings or errors, it just doesn't call the method.

If i copy and paste the method instead of calling it, it works fine so it's not a problem with the variables, it seems there is just an error running the method if it's in a different class?

Any help would be greatly appreciated.

Thanks in advance

switchViews method:

- (void)switchViews:(NSWindow*)mainWindow:(NSView*)newView {
    NSView *dummyView;
    [theMainWindow setContentView:dummyView];
    [theMainWindow setContentSize:newView.frame.size];
    [theMainWindow setContentView:newView];
}

EDIT: sorry guys, it was just a typo.

SOLUTION: I was using the wrong parameter in the method call. Below is the working code:

- (void)switchViews:(NSWindow*)mainWindow:(NSView*)newView {
    NSView *dummyView;
    [mainWindow setContentView:dummyView];
    [mainWindow setContentSize:newView.frame.size];
    [mainWindow setContentView:newView];
}

Upvotes: 0

Views: 133

Answers (2)

Peter Hosey
Peter Hosey

Reputation: 96373

MainMenu *theMainMenu = [MainMenu alloc[ init]];

If that's your code, then it isn't even running, because the build fails, because that code is invalid.

Assuming your actual code doesn't contain syntax errors, nothing happening generally means you sent a message to nil. Either -[MainMenu init] returned nil, or the implementation of switchViews:: tried to send a message to an object it doesn't have.

Assuming you don't have any statements in -[MainMenu init] to return nil, the latter case is more probable. Make sure that it creates whatever objects it needs to, or, if you mean for it to be nib-based, make sure that you have hooked up all of your MainMenu object's outlets and that you didn't forget to load the nib.

Edit:

- (void)switchViews:(NSWindow*)mainWindow:(NSView*)newView {
    NSView *dummyView;
    [theMainWindow setContentView:dummyView];
    [theMainWindow setContentSize:newView.frame.size];
    [theMainWindow setContentView:newView];
}

You named the method's first argument variable mainWindow, but then you send messages to theMainWindow.

If the MainMenu object has an outlet named theMainWindow, make sure that that's hooked up, and remove the redundant first argument. Alternatively, kill off the outlet (or whatever theMainWindow is) and use mainWindow.

Also, since the AppDelegate (for some reason) is passing the window to the MainMenu object, make sure the AppDelegate actually has the window. Start by checking the value of “param1” in the AppDelegate code.

Upvotes: 1

bbum
bbum

Reputation: 162722

That code doesn't compile(or, if it does, it isn't doing at all what you want).

This makes no syntactic sense:

MainMenu *theMainMenu = [MainMenu alloc[ init]];

In general, though, you probably don't want to be allocating & initializing a new instance of something that is most likely already instantiated and on the screen.

Reading between the lines, I suspect you would benefit from brushing up on both the foundations of Object Oriented programming and on Cocoa design patterns.

Upvotes: 0

Related Questions