我可以从iOS获取标签元素吗?

时间:2010-09-07 09:27:45

标签: ios objective-c uiview uibutton

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

3 个答案:

答案 0 :(得分:89)

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

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

修改

使用Swift中的特定标记获取视图 -

let view = self.view.viewWithTag(100)

如果您想确保具有特定类型的视图,例如UIButton,您应该检查类型:

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

答案 1 :(得分:5)

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

//现在您可以根据标记值

获取按钮

答案 2 :(得分: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);