Objectice-C之方法、消息和选择器
在第一篇Car的方法中加入一个类方法:
+ (NSString *) motto
{
return(@"Ford Prefects are Mostly Harmless");
}
#import <UIKit/UIKit.h>
#import "Car.h"
//统一定义颜色和button风格
#define COOKBOOK_PURPLE_COLOR [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
//定义TestBedViewController控制器
@interface TestBedViewController : UIViewController
@end
//实现控制器
@implementation TestBedViewController
//定义函数
- (void) action: (id) sender
{
// Calling a class method
NSLog(@"%@", [Car motto]);//调用类函数
}
- (void) viewDidLoad
{
self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
self.navigationItem.rightBarButtonItem = BARBUTTON(@"Action", @selector(action:));//设定右边导航栏的按钮的标题和事件
//此处@selector表示选择器
}
@end