How can I go back to the previous view controller? - objective-c

When user click "OK" button from UIAlertController popup, it will go back to the previous view controller.I got stuck how to that.Below this is my code.
if (jsonData == nil){
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Error!" message:#"This Git repository is empty" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
}

Try this -
if (jsonData == nil){
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Error!" message:#"This Git repository is empty" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
// Ok action example
[self.navigationController popViewControllerAnimated:YES];
}];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
}

Related

UiAlertController disappears automatically, without user input

I am attempting to present an warning UIAlertAction when a user has not enter text into a particular field. When the field is missing text and the user presses send, the alert appears for a moment then dismisses.
#pragma mark - Actions
- (IBAction)sendPressed:(id)sender
{
if (_titleLabel.text.length == 0)
{
self.alert = [UIAlertController alertControllerWithTitle:#"Sorry"
message:#"Please enter a title, it is required"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
[self.alert addAction:defaultAction];
[self presentViewController:self.alert animated:YES completion:nil];
}
else
{
}
if (_delegate && [_delegate respondsToSelector:#selector(reportControllerDidPressSend:)]) {
[_delegate reportControllerDidPressSend:self];
}
Are you aware, that you have a empty else branch and an independent second if statement? I am quite sure, you want an else if-state meant instead
Also the test if delegate is nil isn't needed.
#pragma mark - Actions
- (IBAction)sendPressed:(id)sender
{
if (_titleLabel.text.length == 0) {
self.alert = [UIAlertController alertControllerWithTitle:#"Sorry"
message:#"Please enter a title, it is required"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
[self.alert addAction:defaultAction];
[self presentViewController:self.alert animated:YES completion:nil];
} else if([_delegate respondsToSelector:#selector(reportControllerDidPressSend:)])) {
[_delegate reportControllerDidPressSend:self];
}
}

Why when using GCD, UIAlertController display is delayed or not shown?

I have this code, which gets called from another class like this:
// send message not to turn off machine and display network indicator
[cm displayAlert: #"Warning" andData: #"Database restore is about to begin; DO NOT turn iPad OFF during restore!" andTag:0 andViewController:self];
and this is the code:
#pragma mark - displayAlert
- (void) displayAlert: (NSString *)alertTitle andData: (NSString *) alertMessage andTag: (int) tag andViewController: vc {
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:alertTitle
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"OK", #"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"OK action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
dispatch_async(dispatch_get_main_queue(), ^{
[vc presentViewController:alertController animated:YES completion:nil];
});
}
The problem is that the presentation is either delayed or not shown at all. I used the GCD code which many, if not all, of the examples on SO show... what could be causing this delay?

UIAlertController not allowing click

I am generating a UIAlertController for use in iOS8 to replace a UIActionSheet function.
The controller presents correctly, but I cannot click anywhere in the controller to get a response. I put in simple NSLog into the handler, and it is absolutely not firing.
Can anyone shed some light on this problem?
BTW, in iOS8 the UIActionSheet is ALSO not generating click events, but in iOS7 this works fine with absolutely no code change.
here is the code:
NSString *alertMessage = #"How do you wish to share ?";
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Share Image"
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
// You can add as many actions as you want
UIAlertAction *emailShare = [UIAlertAction actionWithTitle:#"Share using email"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"EMAIL");
}];
UIAlertAction *smShare = [UIAlertAction actionWithTitle:#"Share using twitter/facebook"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self actionSheet:nil didDismissWithButtonIndex:1];
}];
UIAlertAction *cancelShare = [UIAlertAction actionWithTitle:#"cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self actionSheet:nil didDismissWithButtonIndex:2];
}];
// Add actions to the controller so they will appear
[alertController addAction:emailShare];
[alertController addAction:smShare];
[alertController addAction:cancelShare];
// Finally present the action
[self presentViewController:alertController animated:YES completion:nil];
You have a few different issues with your code. I've commented on the changes that are necessary. Let me know if you have any questions.
// No need to create a NSString. Just set the message.
// NSString *alertMessage = #"How do you wish to share?";
// If you'd like a UIActionSheet use UIAlertControllerStyleActionSheet as the preferredStyle
// Opposed to UIAlertControllerStyleAlert, which presents a UIAlertView
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Share Image"
message:#"How do you wish to share?" // Message Set
preferredStyle:UIAlertControllerStyleActionSheet]; // Changed to show action sheet
UIAlertAction *emailShare = [UIAlertAction actionWithTitle:#"Share using email"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
// Call your function here
// ie. [self emailShare];
NSLog(#"First Button");
}];
UIAlertAction *smShare = [UIAlertAction actionWithTitle:#"Share using twitter/facebook"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// [self actionSheet:nil didDismissWithButtonIndex:1];
// Where did you create actionSheet?
// didDismissWithButtonIndex: is not neccessary
// Just simply dismiss the UIAlertController
[alertController dismissViewControllerAnimated:YES completion:nil];
// And then call your function here
// ie. [self twitterShare];
NSLog(#"Second Button");
}];
UIAlertAction *cancelShare = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel // Set style to cancel
handler:^(UIAlertAction *action) {
// [self actionSheet:nil didDismissWithButtonIndex:2];
// Where did you create actionSheet?
// didDismissWithButtonIndex: is not neccessary
// Just simply dismiss the UIAlertController
[alertController dismissViewControllerAnimated:YES completion:nil];
NSLog(#"Cancel Button");
}];
[alertController addAction:emailShare];
[alertController addAction:smShare];
[alertController addAction:cancelShare];
[self presentViewController:alertController animated:YES completion:nil];

presentViewController not working in viewDidLoad

In this code the alert action is shown every time the app become active:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Please make your choice"
message:#"Would you like a cup of coffee?"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *yesAction = [UIAlertAction actionWithTitle:#"YES"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"You tapped YES");
}];
UIAlertAction *maybeAction = [UIAlertAction actionWithTitle:#"MAYBE"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"You tapped MAYBE");
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel
handler:nil];
[alertController addAction:yesAction];
[alertController addAction:maybeAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
Also, everything works as expected if I move the code block of the UIAlertController in viewDidAppear method.
But if I move the UIAlertController in viewDidLoad :
- (void)viewDidLoad {
UIAlertController *alertController [...]
[...]
[self presentViewController:alertController animated:YES completion:nil];
}
it doesn't works. The alert is not shown.
In viewDidLoad it is not part of the view hierarchy at that time hence it is silently ignored, on viewWillAppear at this time the view hierarchy is already set up hence the reason it works.

UIAlertController showing with delay

I'm experiencing a problem with UIAlertController on my app now migrated to iOS8 with Date Picker inside.
Below is the code.
UIAlertController *AlertView = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *ok = [UIAlertAction actionWithTitle:#"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
[AlertView dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *set = [UIAlertAction actionWithTitle:NSLocalizedString(#"Set to today", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
[self set_to_today:nil];
[AlertView dismissViewControllerAnimated:YES completion:nil];
[self.tableView reloadData];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
[AlertView dismissViewControllerAnimated:YES completion:nil];
}];
UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setDate:data_appo];
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[AlertView.view addSubview:datePicker];
[AlertView addAction:ok];
[AlertView addAction:set];
[AlertView addAction:cancel];
[self.view bringSubviewToFront:datePicker];
[self presentViewController:AlertView animated:YES completion:nil];
UIAlertController and Date Picker is shown when the user select a row from UITableViewController.
The problem is the following:
first time the users select the row everything works fine...but if the user select "Cancel" and then select de tate again the UIAlertController takes 2-3 seconds to show up...this happens also in the simulator...
I'm getting crazy....this makes my app have a bad user experience.
Any help will be strongly appreciated
Thanks
Alex
I was having the same issue with a UIAlertController presented by selecting a row from a UITableView. The first time everything worked fine, and then when the user triggered the alert again there was a few seconds delay before the alert was actually presented.
As a workaround I used GCD:
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:AlertView animated:YES completion:nil];
});
It is probably a bug since -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath is already executed on the main thread.
I submitted a bug report to Apple: rdar://19285091
DispatchQueue.main.async {
self.present(alertView, animated: true, completion:nil)
}
Swift 3.0 version. Alternatively, setting animated: false also solved my problem.