xcode 在其他 .xib 中获取按钮标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8803964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:57:38  来源:igfitidea点击:

to get button tag in other .xib

xcodeuibutton

提问by Krunal

I have one button in sample.xib named "Google", and I want to load google page in sample1.xib using webview. I may have more buttons like fb,youtube,etc. Now, I want that which button is clicked and according to that, sample1.xib display the webpage. What should I do to get text or id of button ?

我在 sample.xib 中有一个名为“Google”的按钮,我想使用 webview 在 sample1.xib 中加载 google 页面。我可能有更多按钮,例如 fb、youtube 等。现在,我想要点击哪个按钮,并根据那个,sample1.xib 显示网页。我应该怎么做才能获取按钮的文本或 ID?

采纳答案by Kjuly

Set tagfor your buttons and treat it as an ID(Note: only number is valid).

tag为您的按钮设置并将其视为 ID(注意:只有数字有效)。

[fbButton setTag:1];
[fbButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//...

[youtubeButton setTag:2];
[youtubeButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//...

and your button action:

和您的按钮操作:

- (void)buttonAction:(id)sender
{
    switch (((UIButton *)sender).tag) {
        case 1:
            // load facebook view
            break;

        case 2:
            // load youtube view
            break;

        default:
            break;
    }
}

回答by Abizern

Using tags is a little clunky. You have another thing to keep track of. There are two ways of doing this.

使用标签有点笨拙。你还有另一件事要跟踪。有两种方法可以做到这一点。

First Way

第一种方式

Use a different action method for each button. This may seem like a lot of repeated code, but I believe in writing code that is easier for the human to read, and letting the compiler take care of optimising (at least until I profile) It makes it a lot easier to see exactly what each method does and an action name like openGoogleInWebviewis a lot more informative than buttonAction

对每个按钮使用不同的操作方法。这可能看起来像很多重复的代码,但我相信编写更易于人类阅读的代码,并让编译器负责优化(至少在我分析之前)它可以更容易地看到究竟是什么每个方法都这样做,并且一个动作名称openGoogleInWebviewbuttonAction

So with this - wire up each buttons action (it's quite easy in Interface Builder) to each action:

因此,有了这个 - 将每个按钮操作(在 Interface Builder 中很容易)连接到每个操作:

- (IBAction)openGoogleInWebview;
- (IBAction)openYouTubeInWebview;

etc.

等等。

See - and if you want to optimise this you can write a method that takes a URL and opens it in the webview and send the URL to that: For example

看到 - 如果你想优化它,你可以编写一个方法来获取一个 URL 并在 webview 中打开它并将 URL 发送到:例如

- (IBAction)openGoogleInWebview {
    NSURL *url = // Your URL here;
    [self openWebviewWithURL:url];
}
...
- (void)openWebviewWithURL:(NSURL *)url {
    // load your webview with the url
}

Keeps things compartmentalised, readable and easy to maintain.

将事物分隔开来,可读且易于维护。

Second Way

第二种方式

If you really want to go with the single method that depends on the button that is pressed, there is no reason to use the tag of the button. Which isn't maintainable. You already have a unique identifier for each button; it's pointer address.

如果您真的想使用依赖于按下的按钮的单一方法,则没有理由使用按钮的标签。这是不可维护的。您已经拥有每个按钮的唯一标识符;它是指针地址。

If you create IBOutlets for your buttons in your view controller, all you need to do is check the pointer:

如果您在视图控制器中为您的按钮创建 IBOutlets,您需要做的就是检查指针:

.h

。H

@property (weak, nonatomic) IBOutlet UIButton *googleButton;
@property (weak, nonatomic) IBOutlet UIButton *youTubeButton;

.m

.m

- (IBAction)buttonClicked:(id)sender {
    if (sender == googleButton) {
        // load Google
    } else if (sender == youTubeButton) {
        // load YouTube
    } // etc
    ...
}

Again - more maintainable; you don't have to keep track of the button's tag. And the code is much more readable because you can see that the google button loads the google page, which is cleaner than saying that tag1 opens the google page and tag 1 is supposed to be the tag for the google button.

再次 - 更易于维护;您不必跟踪按钮的标签。并且代码更具可读性,因为您可以看到 google 按钮加载了 google 页面,这比说 tag1 打开 google 页面和 tag 1 应该是 google 按钮的标签更清晰。

回答by Krunal

I have got the solution....declare variable in second view...and use it in first view....and get tag from first view of tag....:)

我得到了解决方案......在第二个视图中声明变量......并在第一个视图中使用它......并从标签的第一个视图中获取标签......:)