我可以通过iPhone SDK中的标签获取元素吗?

时间:2022-10-20 21:12:22

I want to get back a button, I've already assign the tag to there, how can I do so? Thank you.

我想取回一个按钮,我已经将标签分配到那里,我该怎么办?谢谢。

3 个解决方案

#1


87  

Use the viewWithTag method. e.g. if your button is in your controller's view and your button's tag is 100 the following line will return the button:

使用viewWithTag方法。例如如果您的按钮位于控制器的视图中,并且按钮的标记为100,则以下行将返回按钮:

UIButton *button = (UIButton *)[self.view viewWithTag:100];

EDIT:

编辑:

Get view with particular tag in Swift -

在Swift中获取带有特定标签的视图 -

let view = self.view.viewWithTag(100)

If you want to make sure you have the specific type of view, say a UIButton, you should check the type:

如果你想确保你有特定类型的视图,比如一个UIButton,你应该检查类型:

if let button = self.view.viewWithTag(100) as? UIButton {
    //Your code
}

#2


5  

UIButton *button=(UIButton *)[self.view viewWithTag:tag]; 

//Now you can get your button based on tag value

//现在,您可以根据标记值获取按钮

#3


0  

//Get View using tag
        UITextField *textFieldInView = (UITextField*)[self.view viewWithTag:sender.tag+1000];
        UILabel *labelInView = (UILabel*)[self.view viewWithTag:sender.tag+2000];

//Print its content based on the tags
        NSLog(@"The tag is %d ", textFieldInView.tag);
        NSLog(@"The tag is %d ", labelInView.tag);
        NSLog(@"The Content is %@ ", textFieldInView.text);
        NSLog(@"The Content is  %@ ", labelInView.text);

#1


87  

Use the viewWithTag method. e.g. if your button is in your controller's view and your button's tag is 100 the following line will return the button:

使用viewWithTag方法。例如如果您的按钮位于控制器的视图中,并且按钮的标记为100,则以下行将返回按钮:

UIButton *button = (UIButton *)[self.view viewWithTag:100];

EDIT:

编辑:

Get view with particular tag in Swift -

在Swift中获取带有特定标签的视图 -

let view = self.view.viewWithTag(100)

If you want to make sure you have the specific type of view, say a UIButton, you should check the type:

如果你想确保你有特定类型的视图,比如一个UIButton,你应该检查类型:

if let button = self.view.viewWithTag(100) as? UIButton {
    //Your code
}

#2


5  

UIButton *button=(UIButton *)[self.view viewWithTag:tag]; 

//Now you can get your button based on tag value

//现在,您可以根据标记值获取按钮

#3


0  

//Get View using tag
        UITextField *textFieldInView = (UITextField*)[self.view viewWithTag:sender.tag+1000];
        UILabel *labelInView = (UILabel*)[self.view viewWithTag:sender.tag+2000];

//Print its content based on the tags
        NSLog(@"The tag is %d ", textFieldInView.tag);
        NSLog(@"The tag is %d ", labelInView.tag);
        NSLog(@"The Content is %@ ", textFieldInView.text);
        NSLog(@"The Content is  %@ ", labelInView.text);