栏按钮项无法工作

时间:2011-11-22 14:00:38

标签: iphone objective-c xcode4 uikit

-(IBAction)startClick:(id)sender{
 stick.highlighted = YES;
}

-(void)viewDidLoad{  
 [super viewDidLoad];  
 stick = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ball1.png"]];
 stick.userInteractionEnabled= YES;

 stick.highlightedImage = [UIImage imagedNamed:@"ball2.png"];
 [self.view addSubview:stick]; 
}
输入viewDidLoad方法后,

按钮不起作用。请帮忙。谢谢。棒与uiimageview相关联。

2 个答案:

答案 0 :(得分:0)

可能是由于:

-(void)viewDidLoad
{  
 [super viewDidLoad];  
 stick = [UIImageView alloc]initWithImage:[UIImage imageNamed:@"ball1.png"];
 stick.userInteractionEnabled= YES;

// stick.highlightedImage = [UIImage imaged:@"ball2.png"];

 stick.highlightedImage = [UIImage imageNamed:@"ball2.png"];
 [self.view addSubview:stick]; 
}

答案 1 :(得分:0)

你好,你说棍子链接到UIImageView,然后它有点含糊不清。目前尚不清楚您是否为此声明了一个IBOutlet并连接到xib中的UIImageview,或者您已将其声明为头文件中的变量。有两种方法都可以正常工作

方法1:

在.h文件中声明一个IBoutlet并将其连接到xib中的Imageview。调用方法如下

-(void)viewDidLoad{  
    [super viewDidLoad];  
    stick.image = [UIImage imageNamed:@"firstImage.png"];
    stick.highlightedImage = [UIImage imageNamed:@"secondImage.png"];
}

-(IBAction)startClick:(id)sender{
    stick.highlighted = YES;
}

将满足您的目的。该方法是在事件中触摸内部的xib中的按钮调用的。如果你想从条形按钮调用这个方法,你只需要将条形按钮的选择器连接到startclick方法。

//第二种方法

如果您已将标题中的Imageview声明为UIImageView * stick;

在viewDidLoad方法中,您需要将其分配为

-(void)viewDidLoad{  
    [super viewDidLoad];  

    if (!stick) {
        stick = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
        [stick setImage:[UIImage imageNamed:@"firstImage.png"]];
        [stick setHighlightedImage:[UIImage imageNamed:@"secondImage.png"]];
    }

    UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithTitle:@"Click" style:UIBarButtonItemStylePlain 
                                                                 target:self action:@selector(startClick:)];
    self.navigationItem.rightBarButtonItem = barButton;
    [barButton release];

}

-(void)startClick:(id)sender{
    stick.highlighted = YES;
}

希望它有所帮助!!