3
A
回答
1
- (IBAction)buttonPressed:(id)sender {
UIButton selectedButton = (UIButton *)sender;
NSLog(@"Selected button tag is %d%", selectedButton.tag);
}
3
您可以使用該標籤獲取您的按鈕的引用。例如,您已將UIButton
s添加到UIView *mainView
。要獲得參考按鈕,你應該寫下列:
UIButton *buttonWithTag1 = (UIButton *)[mainView viewWithTag:1];
7
你需要設置每個按鈕的目標行動。
[button setTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
然後實現someMethod:
這樣的:
- (void)someMethod:(UIButton *)sender {
if (sender.tag == 1) {
// do action for button with tag 1
} else if (sender.tag == 2) {
// do action for button with tag 2
} // ... and so on
}
4
爲什麼你需要使用tag
拿到按鈕。您可以直接從其操作方法獲取按鈕引用。
- (void)onButtonPressed:(UIButton *)button {
// "button" is the button which is pressed
NSLog(@"Pressed Button: %@", button);
// You can still get the tag
int tag = button.tag;
}
我希望你已經添加了按鈕的目標動作。
[button addTarget:self action:@selector(onButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
1
usefully we use btn tag if You Write One Function For (more than one) Buttons .in action if we want to write separate Action For button at that situvation we use btn tag.it can get two ways
I) case sender.tag
//if we have four buttons Add,mul,sub,div having Same selector and add.tag=10
mul.tag=20,sub.tag=30,div.tag=40;
-(IBAction) dosomthing:(id)sender
{
int x=10;
int y=20;
int result;
if(sender.tag==10)
{
result=x+y;
}else if(sender.tag==20)
{
result=x*y;
}else if(sender.tag==30)
{
result=x-y;
}else if(sender.tag==40)
{
result=x/y;
}
NSLog(@"%i",result);
}
2)Case
UIButton *btn=[self.view viewWithTag:10];
then you got object of add button uyou can Hide It With btn.hidden=YES;
3
設置的標籤是這樣的:
for (createButtonIndex=0; createButtonIndex<buttonsCount; createButtonIndex++)
{
buttonCaps.tag=createButtonIndex;
}
而且方法添加到陷阱的標籤: -
-(void)buttonsAction:(id)sender
{
UIButton *instanceButton = (UIButton*)sender;
switch(instanceButton.tag)
{
case 1(yourTags):
//Code
break;
case 2:
//Code
break;
}
}
希望這有助於!
0
UIButton *btn = (UIButton *)[mainView viewWithTag:button.tag];
相關問題
- 1. 如何從一組按鈕中獲取所有按鈕標記值
- 2. 在其他.xib中獲取按鈕標記
- 3. YUI按鈕onclick獲取值
- 4. QTP - 獲取按鈕值
- 5. 如何從iPhone sdk中的標記獲取文本字段值?
- 6. iPhone取消按鈕
- 7. iPhone MPMoviePlayer獲取按鈕視圖
- 8. 獲取iPhone GO按鈕提交表格
- 9. UIAlertView按鈕標記
- 10. 如何獲取jquery中的按鈕值
- 11. 按標題(或標記)獲取ContentControl
- 12. 從按下按鈕獲取返回值
- 13. jQuery的按鈕 - 按下獲取價值
- 14. 如何獲取單選按鈕在ListView中的值在Android中單擊按鈕
- 15. 獲取值標記中的pdo pdo HTML
- 16. iphone - 在分段控件中獲取按鈕id
- 17. 如何刪除iPhone中的按鈕標記重複?
- 18. 使用DOMDocument獲取標題標記值
- 19. ASP.NET CreateUserWizard取消按鈕在標記中不可見
- 20. 如何在頁面標記中獲取標籤文本值?
- 21. 獲取按鈕的相關標籤
- 22. 如何獲取MessageBox按鈕標題?
- 23. 閃光按鈕:獲取框架標籤?
- 24. 獲取標記
- 25. 獲取單選按鈕值選中或取消選中jQuery中
- 26. 按鈕標記提交
- 27. 語義按鈕標記
- 28. 獲取按鈕值時點擊Angularjs
- 29. 用Javascript獲取單選按鈕值
- 30. 如何從按鈕獲取數據值?
如果在任何視圖中沒有添加Button,那麼可以使用標籤值檢索該按鈕? – NiKKi
@NiKKi「如果沒有添加」...「可以檢索到」...你想要檢索什麼? – Nekto