Apple已棄用iOS 8.3中的操作表,您如何將操作表添加到我的用戶界面?iOS 9中的操作表問題
我意識到Apple的文檔不太清楚如何使用UIAlertController創建操作表。所以在玩了一段時間之後,我只想分享我的代碼,因爲我無法在Stack Exchange上找到有關此主題的任何有用內容。
Apple已棄用iOS 8.3中的操作表,您如何將操作表添加到我的用戶界面?iOS 9中的操作表問題
我意識到Apple的文檔不太清楚如何使用UIAlertController創建操作表。所以在玩了一段時間之後,我只想分享我的代碼,因爲我無法在Stack Exchange上找到有關此主題的任何有用內容。
我在我的iPhone應用程序相同的問題,與UIActionSheet
詢問用戶,如果他們想拍照,或從他們自己的相冊中選擇一個圖片。
到iOS 9之前,下面的代碼工作正常:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Take photo", @"Choose Existing", nil];
[actionSheet showInView:self.view];
然而,在iOS版9,這一切都完全變暗屏幕,沒有什麼會出現。
是的......謝謝蘋果。
解決方法是用UIAlertController
替換UIActionSheet
。
UIAlertController* alert = [UIAlertController
alertControllerWithTitle:nil // Must be "nil", otherwise a blank title area will appear above our two buttons
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* button0 = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
// UIAlertController will automatically dismiss the view
}];
UIAlertAction* button1 = [UIAlertAction
actionWithTitle:@"Take photo"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// The user tapped on "Take a photo"
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}];
UIAlertAction* button2 = [UIAlertAction
actionWithTitle:@"Choose Existing"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// The user tapped on "Choose existing"
UIImagePickerController *imagePickerController= [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}];
[alert addAction:button0];
[alert addAction:button1];
[alert addAction:button2];
[self presentViewController:alert animated:YES completion:nil];
請注意,如果你不包括取消選項(與UIAlertActionStyleCancel
風格的選項),然後動作片會出現,但動作片之外的任何竊聽不會解僱菜單。
有一個問題:
即使我們指定我們希望這個UIAlertController
有風格UIAlertControllerStyleActionSheet
,你需要設置標題nil
,而不是一個空字符串,否則你會得到一個醜陋的差距在窗口的頂部。
我迫不及待地想看到什麼完美的,工作代碼的iOS 9.2將打破...
更新
我的評論:「然而,在iOS版9,所有這一切都是完全變暗的屏幕,並沒有什麼會出現「有點不對。
實際上,當屏幕鍵盤可見時,我正在打開我的UIActionSheet
。而在iOS 9中,鍵盤將出現在您的UIActionSheet
的之上,因此您不能再看到您的動作表(!!),但您的做了看到屏幕的其餘部分變暗了。
隨着UIAlertController
,iOS更方便用戶,因爲它隱藏屏幕鍵盤嘗試顯示在屏幕底部的操作表之前。究竟爲什麼蘋果不會這樣做UIActionSheets
超出了我的想象。
(嘆氣)。
請允許我回去使用Visual Studio,現在..?
另一個更新
蘋果已經表示,UIActionSheets現在 「棄用的iOS 8」。
但是,如果你想繼續使用他們,在包含文本框您的iOS屏幕,那麼解決方法這個錯誤,errr,問題,是顯示您UIActionSheet
前添加一行代碼:
[self.view endEditing:true];
這可以確保屏幕鍵盤在顯示您的「棄用的」操作表之前被解除。
(嘆氣)如果有人需要我,我會在酒吧。
以下是我在應用程序中爲操作表使用的代碼片段,以防萬一任何人需要幫助,試圖找出如何將UIAlertController用作操作表。
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Action Sheet"
message:@"This is an action sheet."
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *button1 = [UIAlertAction actionWithTitle:@"Button Title 1" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//code to run once button is pressed
}];
UIAlertAction *button2 = [UIAlertAction actionWithTitle:@"Button Title 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//code to run once button is pressed
}];
[alert addAction:button1];
[alert addAction:button2];
[self presentViewController:alert animated:YES completion:nil];
在iOS 9和iPad上,我不得不添加一些重要的更改。可能將來有人可能需要這個。
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Server"
message:@"Choose server to point to"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *button1 = [UIAlertAction actionWithTitle:@"Development" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//code to run once button is pressed
}];
UIAlertAction *button2 = [UIAlertAction actionWithTitle:@"Production" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//code to run once button is pressed
}];
[alert addAction:button1];
[alert addAction:button2];
UIPopoverPresentationController *popPresenter = [alert popoverPresentationController];
popPresenter.sourceView = self.view;
//To present the actionsheet the bottom of screen
popPresenter.sourceRect = CGRectMake(self.createBtn.frame.origin.x, self.view.frame.size.height, self.createBtn.frame.size.width, self.createBtn.frame.size.height);
alert.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:alert animated:YES completion:nil];
感謝您加入到此主題Mike! –
謝謝德里克。當我發佈像這樣的有用回覆時,我似乎在StackOverflow上得到了很多操作,所以很高興獲得一次好的反饋! –
嘿邁克不是問題!我確實有一個與此線程無關的問題,但是您知道是否可以構建一個應用程序,通過藍牙解鎖車門?我找不到任何東西,可能不可能,但我想我會問你。 –