开发者

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 : UI开发者_Python百科ViewController {   
    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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜