两个 ViewController - 第一个有 x 个按钮,第二个有 x 个标签
我遇到了一个小问题(毫不奇怪,因为我刚刚开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个方便您使用的小技巧:标签。
每个
UIView
都可以有一个属性标签
。它是一个简单的整数,您可以在代码 (button.tag = 456;
) 或 Interface Builder 中分配它。在您的switch
方法中,只需使用:所以只是为了确保:您的声明
是完全错误的。如果新的视图控制器有一个@property(您在
.h
文件中定义,在.m
文件中定义@synthesize
) ,您可以在推送新的视图控制器之前简单地分配这些属性。这就是我们在上面的代码片段中所做的。Here is a handy little trick for you: tags.
Every
UIView
can have a propertytag
. It is a simple integer, and you can assign it in code (button.tag = 456;
) or in Interface Builder. In yourswitch
method, simply use:So just to make sure: your statement
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.