drw
drw

Reputation: 943

calling view methods from a view controller

This is a simple question, and I'm sure I got it work in the past. I'm trying to call a method in a view

#import <UIKit/UIKit.h>

@interface View : UIView {
}

-(void)spinAction;

@end

from a view controller

#import <UIKit/UIKit.h>
#import "View.h"

@interface layers3ViewController : UIViewController {   
    IBOutlet View *view;
}

-(IBAction)spinButton:(id)sender;

@end

via the method

-(IBAction)spinButton:(id)sender{
    [view spinAction];
}

but cannot get to it work. Everything is put together using the Interface Builder. Am I doing something fundamentally wrong here? A previous code worked by putting

[self.view spinAction]

albeit with error messages but not even that works here. Any hints / suggestions more than welcome.

Upvotes: 1

Views: 111

Answers (2)

grahamparks
grahamparks

Reputation: 16296

self.view returns an object of class UIView, and you probably shouldn't try to change that. You need to create a new outlet with a class of View, and wire it up in interface builder.

@interface layers3ViewController : UIViewController {   
    IBOutlet MyView *myView;
}
@property (nonatomic,retain) IBOutlet MyView *myView;


-(IBAction)spinButton:(id)sender;

@end

In your implementation should look like this:

@implementation layers3ViewController
@synthesize myView;
-(IBAction)spinButton:(id)sender{
    [self.myView spinAction];
}

Don't forget to connect the myView outlet in Interface Builder to your view.

Upvotes: 1

filipe
filipe

Reputation: 3380

view is already a property of UIViewController and it is an IBOutlet so I'm not sure wich view would be set when you make the association in interface builder. Try this

#import <UIKit/UIKit.h>
#import "View.h"

@interface layers3ViewController : UIViewController {   
    //IBOutlet View *view;
}

-(IBAction)spinButton:(id)sender;

@end

and

-(IBAction)spinButton:(id)sender
{
    View *myView = (View *)self.view;
    [myView spinAction];
}

and of course in Interface Builder make sure the object your associating to the view outlet is an instance of your View class.

Upvotes: 2

Related Questions