2016-02-29 25 views
-1

HeyI想要做到這一點我的按鈕和按鈕中有文本框我想這樣做,當我按下按鈕動作選取器成爲出現,並給出4至5列表的字符串無論我選擇它會在按鈕上的文本框中顯示。請通過添加一個目標你的按鈕幫我按鈕上的操作表選擇器IOS

enter image description here

+0

當點擊動作片按鈕設置的TextField.text = @,你打 – sohil

+0

,如果這是要在iPad上運行,一必須聲明'popoverPresentation'。通常是一個欄按鈕項目,或者定義一個'sourceRect'和一個'sourceView'。它會在iPad上崩潰,否則 –

回答

1

開始。在Objective-C中,這將是這樣的:

[myButton addTarget:self 
      action:@selector(buttonPressed:) 
    forControlEvents:UIControlEventTouchUpInside]; 

然後創建方法buttonPressed。這方面的一個例子是:

- (void)buttonPressed:(id)sender { 
    if ([sender isEqual:self.myButton]) { 
     //This is where you can create the UIAlertController 
    } 
} 

然後,創建UIAlertController

UIAlertController *myAlertController = [UIAlertController alertControllerWithTitle:@"Title" 
                    message:@"Message" 
                   preferredStyle:UIAlertControllerStyleActionSheet]; 

然後你創建你想有每個按鈕出現在動作片的動作。你需要有一個按鈕的標題和一個動作,儘管動作塊可以是空的。

UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"Action 1" 
                style:UIAlertActionStyleDefault 
               handler:^(UIAlertAction *action) { 
                //Whatever you want to have happen when the button is pressed 
               }]; 
[myAlertController addAction:action1]; 

//repeat for all subsequent actions... 

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) 
                 style:UIAlertActionStyleCancel 
                handler:^(UIAlertAction *action) { 
                 // It's good practice to give the user an option to do nothing, but not necessary 
                }]; 
[myAlertController addAction:cancelAction]; 

最後,你目前的UIAlertController

[self presentViewController:myAlertController 
        animated:YES 
       completion:^{ 

       }]; 

注:

如果您正在爲iPad和使用動作片風格的UIAlertController,那麼你將需要設置UIAlertController的來源。這是可以做到這樣的:

if ([sender isKindOfClass:[UIView class]]) { 
    if ([myAlertController.popoverPresentationController respondsToSelector:@selector(setSourceView:)]) { // Check for availability of this method 
     myAlertController.popoverPresentationController.sourceView = self.myButton; 
    } else { 
     myAlertController.popoverPresentationController.sourceRect = self.myButton.frame; 

    } 
} 
+0

另外值得注意的「按鈕標題」 –

+0

這是一個很好的觀點。我會將該註釋添加到我的答案中。 – Fennelouski