When creating an action sheet with UIAlertController, I'm seeing a top bar always show. This SO post suggests setting the title to nil - this might work on iOS 8.0, but I'm seeing a top bar on iOS 9.0.
Set message to nil also:
UIAlertController *actionSheet= [UIAlertController
alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *actionSheetButton1 = [UIAlertAction
actionWithTitle:#"Button 1"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(#"Button 1 pressed");
}];
UIAlertAction *actionSheetButton2 = [UIAlertAction
actionWithTitle:#"Button 2"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSLog(#"Button 2 pressed");
}];
UIAlertAction *actionSheetButton3 = [UIAlertAction
actionWithTitle:#"Close Button"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
NSLog(#"Close Button pressed");
}];
[actionSheet addAction:actionSheetButton1];
[actionSheet addAction:actionSheetButton2];
[actionSheet addAction:actionSheetButton3];
[self presentViewController:actionSheet animated:YES completion:nil];
Do you also set message to nil?
This should do the trick, at least it works for me iOS9 (iPhone 6):
[UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
Related
I am wondering how to select an option from a picker view, and have that choice appear in a new view controller. For example, I want to select a city in a drop down menu and have that city appear in a label in a new view controller. The picture is how I coded to create the picker view. enter image description here
Instead of dropdown you can use a UIAlertCocntroller with style actionsheet. Place the ALertAction in for loop so as to make cities appear in a list. Perform all this on a button click.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Title" message:#"" preferredStyle:UIAlertControllerStyleActionSheet];
for(NSString *str array){
UIAlertAction *button = [UIAlertAction actionWithTitle:str style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self.button setTitle:str forState:UIControlStateNormal];
}];
[alert addAction:button];
}
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
in my app i have tableview and for clear all cells i given an action to clear button.
button work correctly and all cell deleted. but i want to give a option to user to canceled
i used UIAlertController class and its work only one time after i chosen delete button.(its work every time if i chosen cancel button)
warning in console:
Warning:
Attempt to present UIAlertController on HistoryViewController which is already presenting UINavigationController
- (IBAction)showNormalActionSheet:(id)event
{
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
UIAlertAction *resetAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Clear all recent", #"Reset action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
[self onDeleteClick];
NSLog(#"Reset action");
}];
[alertController addAction:resetAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
//- (IBAction)onDeleteClick:(id) event {
- (void)onDeleteClick {
linphone_core_clear_call_logs([LinphoneManager getLc]);
[tableController loadData];
editButton.hidden = ([tableView.dataSource tableView:tableView numberOfRowsInSection:0] == 0);
if(editButton.selected) {
[editButton toggle];
[self onEditClick:nil];
}
}
I'm trying to change the background highlight color for my alertcontroller. For eg in the below image, right now the default background highlight for a cell is light gray, I need to change it to some other color. I saw various post on changing the tint color but I dont need to change the title color only the background highlight. Is this possible?
//This is my current code
- (IBAction)showAlert:(UIButton *)sender {
UIAlertController *alert =[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* save = [UIAlertAction
actionWithTitle:#"Save"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Perform some action
}];
UIAlertAction* saveas = [UIAlertAction
actionWithTitle:#"Save As"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Perform some action
}];
UIAlertAction* discard = [UIAlertAction
actionWithTitle:#"Discard"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action)
{
//Perform some action
}];
[alert addAction:save];
[alert addAction:saveas];
[alert addAction:discard];
[alert setModalPresentationStyle:UIModalPresentationPopover];
UIPopoverPresentationController *popPresenter = [alert popoverPresentationController];
popPresenter.sourceView = sender;
popPresenter.sourceRect = sender.bounds;
[self presentViewController:alert animated:YES completion:nil];
}
I found one weird behavior of UIAlertController in Objective-C. I have added two actions in it but I found when user selects any action - handler call back get call after approx 1 sec. I am wondering why this delay is happening to get call back. Is it possible to avoid this by any way?
- (IBAction)openAlert:(id)sender {
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"Info"
message:#"You are using UIAlertController"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okBtn = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// Delay ~ 1 sec to get control here
self.view.backgroundColor = [UIColor blackColor];
}];
UIAlertAction* cancelBtn = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action)
{
}];
[alert addAction:okBtn];
[alert addAction:cancelBtn];
[self presentViewController:alert animated:YES completion:nil];
}
Its on main thread. To make sure I tried below code snippet.
dispatch_async(dispatch_get_main_queue(), ^{
// Alert view code
});
I'm trying to display a UIAlertController with some action buttons. It works fine on the iPhone, because it pops from the bottom of the device. I have a problem with the ipad, the UIAlertController does not center properly. It shows a bit off to the right. I can subtract from the x coordinate by subtracting say 150f. Is there a way to just get it center?
UIAlertController * view= [UIAlertController
alertControllerWithTitle:#"My Title"
message:#"Select your Choice"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];
}];
[view addAction:ok];
[view addAction:cancel];
view.popoverPresentationController.sourceView = self.view;
view.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0, 0.0, 0.0);
[self presentViewController: view animated:YES completion:nil];
If you have simple actions then better to use Alertstyle, it centers automatically or if you insist to use Actionsheet style, try setting popoverPresentationController.permittedArrowDirections = 0 this works nice with a fixed orientation but fails if you rotate in iPad.
// Alert
- (void) showAlert
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"TEst" message:#"test Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:cancelAction];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}