UIAlertController : How to show all separator lines when added much actions? - ios

When UIAlertController cannot show all action on screen, it can scroll.
But, separators disappears.
What should I do to display all separators?
iOS : 8.4
Xcode : 6.4
Example:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"test" message:#"test" preferredStyle:UIAlertControllerStyleActionSheet];
for ( int i = 0; i < 50; i++ ) {
[alertController addAction:[UIAlertAction actionWithTitle:[NSString stringWithFormat:#"%d", i] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}]];
}
[self presentViewController:alertController animated:YES completion:^{
}];

Related

UIAlertController perform once after use UIAlertActionStyleDestructive button?

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];
}
}

PresentViewController is not working in objective c. App is crashing

When I am trying to present alert view controller my app is crashing.
-(void)setupAlertCtrl{
self.alertCtrl=[UIAlertController alertControllerWithTitle:#"Select Image"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
}
- (IBAction)selectImagePressed {
**[self presentViewController:self.alertCtrl
animated:YES
completion:nil];//this funcanility is breaking**
}
Exception :
libc++abi.dylib: terminating with uncaught exception of type NSException
libc++abi.dylib: terminating with uncaught exception of type NSException
This could also happen if you've wired up a button to an IBAction that doesn't exist anymore (or has been renamed)"
If you're running into this problem than go to your storyboard, RIGHT click on the yellow box icon (view controller) at the top of the phone outline and DELETE the outlet(s) with yellow flags.
What happens in instances like this is you probably named an action, then renamed it.
Hope this will help you.
This code will definitely help you.
I suspect you are using iPad(normal action sheet crashes in iPad)
-(void)setupAlertCtrl
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:#"Your message" preferredStyle:UIAlertControllerStyleActionSheet];
// Remove arrow from action sheet.
[alertController.popoverPresentationController setPermittedArrowDirections:0];
//For set action sheet to middle of view.
CGRect rect = self.view.frame;
rect.origin.x = self.view.frame.size.width / 20;
rect.origin.y = self.view.frame.size.height / 20;
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.sourceRect = rect;
[self presentViewController:alertController animated:YES completion:nil];
}
else
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:#"Your message" preferredStyle:UIAlertControllerStyleActionSheet];
[self presentViewController:alert animated:YES completion:nil];
}
}
you have to call the below function in view did load
-(void)setupAlertCtrl
Then you can open the alert on button click
You should have to write it like,
-(void)setupAlertCtrl
{
self.alertCtrl=[UIAlertController alertControllerWithTitle:#"Select Image"
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
[self presentViewController:self.alertCtrl animated:YES completion:nil];
}
-(IBAction)selectImagePressed
{
[self setupAlertCtrl];
}
Hope this will help you.
UIAlertController is a functionally identical, block-based replacement for both UIAlertView and UIActionSheet. Switching between an alert or action sheet is done by setting the preferred style when creating the controller.
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:alertTitle
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertController animated:YES completion:nil];
I hope this will help you...
Its not crashing,
Just call the SetupAlertCntrl in viewDidLoad method.
Its working fine
You need to check your alertController exist or not first, if not let created it first. One more thing, UIAlertControl only can present from UIViewController so, make sure self in selectImagePressed function is kind of UIViewController.
- (IBAction)selectImagePressed {
if(self.alertController == nil) {
[self setupAlertCtrl];
}
[self presentViewController:self.alertCtrl
animated:YES
completion:nil];//this funcanility is breaking**
}
Try below code:
-(void)setupAlertCtrl
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Choose Profile Image " message:#" " preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* cameraAction = [UIAlertAction actionWithTitle:#"Take Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(#"Camera Action");
}];
[alertController addAction:cameraAction];
UIAlertAction* gallaryAction = [UIAlertAction actionWithTitle:#"Gallary" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(#"Gallary Action");
}];
[alertController addAction:gallaryAction];
UIAlertAction* Cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:Cancel];
[self presentViewController:alertController animated:YES completion:nil];
}
// Button Click action
- (IBAction)selectImagePressed:(id)sender
{
[self setupAlertCtrl];
}
hope this will help you.

How to deal with different UIAlertAction handler in same UIAlertController in iOS?

I am using Objective-C to write about some UIAlertController code.
I have more buttons, but the buttons will show different UIAlertControllers and deal with different UIAlertAction handler .
So I want to create one UIAlertController,and UIAlertAction.
Like below:
-(void) initAlert{
alertController = [UIAlertController alertControllerWithTitle:#"hint" message:#"count down alert" preferredStyle:UIAlertControllerStyleAlert];
doneAction = [UIAlertAction actionWithTitle:#"okey" style:UIAlertActionStyleDefault handler:
^(UIAlertAction *action) {
NSLog(#"show log");
}];
[alertController addAction:doneAction];
}
-(void) showAlert{
[self presentViewController:alertController animated:YES completion:nil];
}
Then I want to using different button IBAction to call the showAlert method, and set different UIAlertController title, UIAlertAction title and deal different alertAction handler.
But I encounter some problems.
I call the method in different button like below:
- (IBAction)btn1Action:(UIButton *)sender {
alertController.title = #"controller 1";
alertController.message = #"message1";
[self showAlert];
}
- (IBAction)btn2Action:(UIButton *)sender {
alertController.title = #"controller 2";
alertController.message = #"message2";
[self showAlert];
}
I don't know how to change the UIAlertAction title with the same doneAction, I search some data show the UIAlertAction is readyonly property.
So have any other methods to change UIAlertAction title? or can we delete the UIAlertController addAction: method to add other UIAlertAction?
And how can I pass different UIAlertAction handler to AlertAction for the same UIAlertController to use?
Thank you very much.
UIAlertController should not be used multiple times. Just use a new UIAlertController instance each time you want to pop up the alert.
- (IBAction)btn1Action:(UIButton *)sender {
[self showAlert:#"Controller 1" message:#"Message 1" handler:^(UIAlertAction *action) {
NSLog(#"btn1Action");
}];
}
- (IBAction)btn2Action:(UIButton *)sender {
[self showAlert:#"Controller 2" message:#"Message 2" handler:^(UIAlertAction *action) {
NSLog(#"btn2Action");
}];
}
-(void)showAlert:(NSString*)alertTitle message:(NSString*)message handler:(void (^ __nullable)(UIAlertAction *action))handler {
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:alertTitle message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * doneAction = [UIAlertAction actionWithTitle:#"Ok" style:UIAlertActionStyleDefault handler:handler];
[alertController addAction:doneAction];
[self presentViewController:alertController animated:YES completion:nil];
}

iOS 9.0 - Remove top bar from UIAlertController action sheet

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];

UIAlertController - iOS 8 - iPad - Not Centering Properly

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];
}

Resources