簡體   English   中英

我可以通過iOS的Tag獲取元素嗎?

[英]Can I get the element by Tag from iOS?

我想找一個按鈕,我已經將標簽分配到了那里,我該怎么辦?

使用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
}
UIButton *button=(UIButton *)[self.view viewWithTag:tag]; 

//現在您可以根據標簽值獲取按鈕

//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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM