1

嗨,我是 XCODE 的新手,正在尝试制作一个使用导航控制器的基于视图的应用程序。在我的一个观点中,我试图为不同的按钮附加相同的操作方法。我正在为 UIButtons 使用标签属性。

代码是

@interface firstview : UIViewController {
    IBOutlet UIButton *button1;
    IBOutlet UIButton *button2;

    }

    @property(nonatomic, retain) IBOutlet UIButton *button1;
    @property(nonatomic, retain) IBOutlet UIButton *button2;

    -(IBAction)push:(UIButton *)sender;

    @end



    #import "firstview.h"
    #import "secondview.h"


    @implementation firstview

    @synthesize button1;
    @synthesize button2;

    -(IBAction)push:(UIButton *)sender{

    button1.tag = 1;
    button2.tag = 2;


    if(sender.tag = button1.tag){
    secondview *v2 = [[secondview alloc]initWithNibName:@"secondview" bundle:Nil];
    v2.title =@"first button";
    v2.l1.text = @"BUTTON1";
    [self.navigationController pushViewController:v2 animated:YES];
    [v2 release];
    }
    else if(sender.tag = button2.tag){
    secondview *v2 = [[secondview alloc]initWithNibName:@"secondview" bundle:Nil];
    v2.title =@"Select";
    v2.l1.text = @"BUTTON2";
    [self.navigationController pushViewController:v2 animated:YES];
    [v2 release];
    }

    }

    @end

我希望当按下特定按钮时,视图的标题和标签的文本都应该改变。但是无论我按下哪个按钮,实现上面的代码都会给我第一个按钮的视图。如果有人能告诉我哪里出了问题,我将不胜感激。

根据建议,我更改了等号。但是标签仍然没有显示文本。

4

1 回答 1

0

你在检查sender.tag = button1.tag,不是sender.tag == button1.tag。您需要使用两个等号来检查是否相等。

发生的事情是您设置sender.tagbutton1.tag. 该代码运行后,程序将评估if (sender.tag). 由于sender.tag将始终等于1, if(sender.tag)== if(1)== YES

于 2012-06-08T03:56:27.183 回答