两个 ViewController - 第一个有 x 个按钮,第二个有 x 个标签

发布于 2024-12-25 07:20:26 字数 1682 浏览 2 评论 0原文

我遇到了一个小问题(毫不奇怪,因为我刚刚开始使用 xcode)。我尝试用 if 语句来解决这个问题,但它们显然是错误的方法。

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

我尝试用标签语句来解决它,例如: 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);

这是我在 SecondViewController.m 中所做的

- (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 的标签 - 有人知道为什么吗?

I run into a small problem (Not surprisingly, 'cause I've just started with xcode). I tried to solve it with if-statemens, but they were clearly the wrong way to go.

Here is what I am trying to do: In the first ViewController I have for example 4 Buttons. If the user presses the first button he gets to ViewController2 and the label says "You pressed the first button". If the user presses the second button he gets to ViewController2 and the label says "You pressed the second button" and so on.

I tried to solve it with Tag Statements, like:
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);

And here what I did in SecondViewController.m

- (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?"];

He always gives me the labels from ButtonTag 9001 - Someone any idea why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔少女心 2025-01-01 07:20:26

这是一个方便您使用的小技巧:标签

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

-(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];
}

所以只是为了确保:您的声明

将属性或值从一个视图控制器传递到另一个视图控制器是不行的

是完全错误的。如果新的视图控制器有一个@property(您在.h文件中定义,在.m文件中定义@synthesize) ,您可以在推送新的视图控制器之前简单地分配这些属性。这就是我们在上面的代码片段中所做的。

Here is a handy little trick for you: tags.

Every UIView can have a property tag. It is a simple integer, and you can assign it in code (button.tag = 456;) or in Interface Builder. In your switch method, simply use:

-(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];
}

So just to make sure: your statement

it is a no go to pass properties or values from one to another view controller

is completely wrong. If the new view controller has a @property (which you define in the .h-file and @synthesize in the .m-file), you can simply assign these properties before pushing the new view controller. That's what we did in the above code snippet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文