iPhone

// Override viewDidLoad method;
-(void)viewDidLoad {
UIImage *buttonImageNormal = [UIImage imageNamed:@"test.png"];
UIImage *stretchableButtonImageNormal =  [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[testButton setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];

UIImage *buttonImagePressed = [UIImage imageNamed:@"icon.png"];
UIImage *stretchableButtonImagePressed =  [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[testButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
}

//--------
减号 –
     代表此方法是实体方法,必须先生成类实例,通过实例才能调用该方法。
加号 +
    代表此方法是类的静态方法,可以直接调用,而不用生成类实例。

// 代码添加Button
LButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,160,50)];
    [LButton setImage:[UIImage imageNamed:@"Button.png"] forState:UIControlStateNormal];
    [LButton addTarget:self action:@selector(LButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:LButton];

//------------------------------------------
如果你在该函数中需要用到这个控件的引用,比如是个UIButton,就最好带上(id)sender,这样你就可以在代码里:UIButton *button = (UIButton*)sender;来获得该按钮的引用,否则就无所谓带不带参数

你可能感兴趣的:(iPhone)