Ios 两个视图控制器-第一个有x个按钮,第二个有x个标签

Ios 两个视图控制器-第一个有x个按钮,第二个有x个标签,ios,label,switch-statement,viewcontroller,Ios,Label,Switch Statement,Viewcontroller,我遇到了一个小问题(毫不奇怪,因为我刚刚开始使用xcode)。我试图用if政治家来解决这个问题,但他们显然走错了路 下面是我要做的:在第一个ViewController中,我有4个按钮。如果用户按下第一个按钮,他将进入ViewController2,标签上会显示“您按下了第一个按钮”。如果用户按下第二个按钮,他将进入ViewController2,标签上会显示“您按下了第二个按钮”,依此类推 我试图用Tag语句来解决这个问题,比如: FirstViewController.m - (IBAct

我遇到了一个小问题(毫不奇怪,因为我刚刚开始使用xcode)。我试图用if政治家来解决这个问题,但他们显然走错了路

下面是我要做的:在第一个ViewController中,我有4个按钮。如果用户按下第一个按钮,他将进入ViewController2,标签上会显示“您按下了第一个按钮”。如果用户按下第二个按钮,他将进入ViewController2,标签上会显示“您按下了第二个按钮”,依此类推

我试图用Tag语句来解决这个问题,比如: FirstViewController.m

 - (IBAction)switch:(id)sender;

 {
 UIButton *buttonPressed = (UIButton *)sender;
SecondViewController *second =[[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
second.buttonTag  = buttonPressed.tag;
[self.navigationController pushViewController:second animated:YES];
(button.tag = 9001);


 - (IBAction)switch2:(id)sender2;

 {
 UIButton *buttonPressed = (UIButton *)sender2;
SecondViewController *third =[[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:third animated:YES];
second.buttonTag  = buttonPressed.tag;
[self.navigationController pushViewController:third animated:YES];
(button2.tag = 9002);
- (void)viewDidLoad
 {
[super viewDidLoad];

if (buttonTag == 9001) {
    self.label1.text = [[NSString alloc] initWithFormat:@"Radnomtext"];
    self.label2.text = [[NSString alloc] initWithFormat:@"Randomtext"];
    self.label3.text = [[NSString alloc] initWithFormat:@"Randomtext?"];

if (buttonTag == 9002) {
    self.label1.text = [[NSString alloc] initWithFormat:@"Radnomtext2"];
    self.label2.text = [[NSString alloc] initWithFormat:@"Randomtext2"];
    self.label3.text = [[NSString alloc] initWithFormat:@"Randomtext2?"];
这里是我在SecondViewController.m中所做的

 - (IBAction)switch:(id)sender;

 {
 UIButton *buttonPressed = (UIButton *)sender;
SecondViewController *second =[[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
second.buttonTag  = buttonPressed.tag;
[self.navigationController pushViewController:second animated:YES];
(button.tag = 9001);


 - (IBAction)switch2:(id)sender2;

 {
 UIButton *buttonPressed = (UIButton *)sender2;
SecondViewController *third =[[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:third animated:YES];
second.buttonTag  = buttonPressed.tag;
[self.navigationController pushViewController:third animated:YES];
(button2.tag = 9002);
- (void)viewDidLoad
 {
[super viewDidLoad];

if (buttonTag == 9001) {
    self.label1.text = [[NSString alloc] initWithFormat:@"Radnomtext"];
    self.label2.text = [[NSString alloc] initWithFormat:@"Randomtext"];
    self.label3.text = [[NSString alloc] initWithFormat:@"Randomtext?"];

if (buttonTag == 9002) {
    self.label1.text = [[NSString alloc] initWithFormat:@"Radnomtext2"];
    self.label2.text = [[NSString alloc] initWithFormat:@"Randomtext2"];
    self.label3.text = [[NSString alloc] initWithFormat:@"Randomtext2?"];

他总是给我ButtonTag 9001的标签-有人知道为什么吗?这里有一个简单的小技巧:标签

每个
ui视图
都可以有一个属性
标记
。它是一个简单的整数,您可以在代码(
button.tag=456;
)或Interface Builder中分配它。在
开关
方法中,只需使用:

-(IBAction)switch:(id)sender {
   UIButton *buttonPressed = (UIButton *)sender;
   // create the second view controller, e.g.
   SecondViewController *secondViewController = [[SecondViewController alloc] init];
   // it should have an NSInteger @property e.g. "buttonTag"
   secondViewController.buttonTag  = buttonPressed.tag
   [self.navigationController 
       pushViewController:secondViewController animated:YES];
   // if not using ACT: [secondViewController release];
}
所以,为了确保:你的声明

无法将属性或值从一个视图控制器传递到另一个视图控制器


这是完全错误的。如果新视图控制器具有@属性(您在
.h
-文件中定义了该属性,在
.m
-文件中定义了
@synthesis
),则只需在推送新视图控制器之前分配这些属性即可。这就是我们在上述代码片段中所做的。

感谢您的快速回答。就像我说的,我对这一点还很陌生,因此我制作并编辑了startpost,因为我不完全理解:-你的“编辑”是一个单独的问题。你的意思是说:
(buttonTag==9001)
。您的单个等号是分配buttonTag,而不是检查是否相等。所以当执行到
时((buttonTag=9002))
buttonTag被分配到9002,这是真的。所以它会两次改变你的标签。对不起,你是对的,但我不想因为它而提出新问题。我将其更改为您的建议,但现在他只显示标签9001中的标签:(