在Xcode中(iPhone應用程序),我創建了一個數字按鈕的編程方式是這樣的:如何按鈕標籤傳遞給prepareForSegue
- (void)viewDidLoad
{
[super viewDidLoad];
// By Road Button
byRoadButton = [UIButton buttonWithType:UIButtonTypeCustom];
byRoadButton.tag = 3;
byRoadButton.frame = CGRectMake(20, 246, 280.f, 40.f);
UIImage *roadButton = [UIImage imageNamed:@"gettingHereByRoadButton.png"];
[byRoadButton setBackgroundImage:roadButton forState:UIControlStateNormal];
[self.view addSubview:byRoadButton];
[byRoadButton addTarget:self action:@selector(byRoadButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}
然後我檢查哪個按鈕被按下的行動:@選擇,並呼籲performSegueWithIdentifier 。 NSLog在這裏顯示正確的按鈕標籤作爲檢查。
- (void) byRoadButtonClicked
{
NSLog(@"Button Pushed Tag Number: %d", byRoadButton.tag);
[self performSegueWithIdentifier:@"gettingHereSegue" sender:self];
}
但是,如何將按鈕標記傳遞給prepareForSegue?我們的想法是將這個標籤傳遞給prepareForSegue,並且基於標籤編號,我將與該按鈕相關的信息傳遞給下一個ViewController?
我曾嘗試:
[self performSegueWithIdentifier:@"gettingHereSegue" sender:byRoadButton.tag];
但得到一個錯誤說我試着去傳遞一個int,而不是一個ID的。
在我prepareForSegue我有以下幾點:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// ThIS IS WHAT I WANT TO DO - PSEUDO CODE
//int tagNumber = sender;
//if (tagNumber == 3)
//{
//Set relevant content of Object here
//}
//THIS IS WHAT I CURRENTLY HAVE - NO CHECK FOR TAG
GettingHereContent *gettingHereContent = [[GettingHereContent alloc] init];
gettingHereContent.content = @"This is my custom content based on this buttons click";
GettingHereViewController *vc = (GettingHereViewController *)segue.destinationViewController;
vc.gettingHere = gettingHereContent;
}
或者任何人都可以提出一個解決方案,或者一個更好的方式來做到這一點嗎?
嘗試[自performSegueWithIdentifier:@ 「gettingHereSegue」 發件人:byRoadButton];然後在prepareForSegue中if(byRoadButton.tagNumber == 3) { //設置Object的相關內容在這裏 } –
是的,工作完美謝謝。然後在prepareForSegue中,我所要做的就是檢查按鈕是否與發件人一樣,如下所示:if(byRoadButton == sender) {//在此處做點什麼}我不必擔心標籤號碼以檢查按鈕點擊。再次感謝 – heyred