Unknown error while moving from one controller to another controller? - ios

In my app i am getting unknown error,while moving from one controller to another by using this code,
-(IBAction)backQA:(id)sender
{
UIAlertController *alert =[UIAlertController alertControllerWithTitle:#"Attention" message:#"Are You Sure Quit The Exam!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
ContestViewController *contestVC =[self.storyboard instantiateViewControllerWithIdentifier:#"ContestVC"];
[self presentViewController:contestVC animated:YES completion:nil];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:ok];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
When i click on my Ok action i am getting error.
Here is the screen shot,
Any help?
Here is the my setMainBanner Method.
- (void)viewDidLoad
{
#try
{
if((NSNull *)[contestDict objectForKey:#"mainbanner"] != [NSNull null])
{
if([[contestDict objectForKey:#"mainbanner"] length] > 0)
{
NSString *mainBannerimage = [contestDict objectForKey:#"mainbanner"];
NSString *bannerImageURL = [NSString stringWithFormat:#"http://www.karo.com/APP/banners/%#", mainBannerimage];
NSURL *imageURL = [NSURL URLWithString:bannerImageURL];
[mainBanner sd_setImageWithURL:imageURL
placeholderImage:[UIImage imageNamed:#"profilepic_bg"]];
}
else
{
}
}
}
#catch (NSException *exception)
{
}
}

Try this:
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];
Finally we can present the alert view controller as with any other view controller:
[self presentViewController:alertController animated:YES completion:nil];

why you are using presentViewController use this
[self dismissViewControllerAnimated:YES completion:nil];

Related

UIAlertView is deprecated : first deprecated in iOS 9.0 - UIAlertView is deprecated. Use UIAlertController with a preferredStyle

How to change this obj c code that suits with iOS 9 above ? i have this error when i updating X Code into 8.2.1 . The code as below
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Continue"]){
NSLog(#"Continue...");
[self performSegueWithIdentifier:#"createlogin" sender:self];
}
}
The error:
UIAlertView is deprecated : first deprecated in iOS 9.0 - UIAlertView
is deprecated. Use UIAlertController with a preferredStyle of
UIAlertControllerStyleAlert instead
Thanks.
UIAlertController * alert = [UIAlertController alertControllerWithTitle:#"Add Title" message:#"Add Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * actionOK = [UIAlertAction actionWithTitle:#"Button Title" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//Here Add Your Action
}];
UIAlertAction * actionCANCEL = [UIAlertAction actionWithTitle:#"Second Button Title" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//Add Another Action
}];
[alert addAction:actionOK];
[alert addAction:actionCANCEL];
[self presentViewController:alert animated:YES completion:nil];
Use this Method if any need ask me and for more Information use this Link http://useyourloaf.com/blog/uialertcontroller-changes-in-ios-8/
Use this code
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Title" message:#"Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * continueButton = [UIAlertAction actionWithTitle:#"Continue" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
[self performSegueWithIdentifier:#"createlogin" sender:self];
}];
UIAlertAction * cancelButton = [UIAlertAction actionWithTitle:#"cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
}];
[alert addAction:continueButton];
[alert addAction:cancelButton];
[self presentViewController:alert animated:YES completion:nil];
try this:
UIAlertController *controller = [UIAlertController alertControllerWithTitle:#"Your message title"
message:#"Enter your message here."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
NSLog(#"Ok Action");
}];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:#"Skip" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
NSLog(#"Cancel Action");
}];
[controller addAction:okAction];
[controller addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];

How to Change the UIAlertAction button Color in iOS?

I've a UIAlertController and I've a bunch of UIAlertAcion Buttons. Now I need to show one button with other Color rather than the same color.
For Ex
Button1
Button2
Button3
Button1 and button3 should be in blue
and
button2 should be in red.
Is it possible ? How?
Just Throw your thoughts
...
My Code:
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:#"Food Menu" message:#"Select the MenuItem" preferredStyle:UIAlertControllerStyleActionSheet];
for(int i= 0; i<[menus count];i++){
UIAlertAction *action = [UIAlertAction actionWithTitle:[menu objectAtIndex:i] style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
[actionSheet dismissViewControllerAnimated:YES completion:nil];
//do SomeWork
}];
if(i==currentIndex){
//Put button Color Red
}
else{
//put button color Blue
}
[actionSheet addAction:action];
}
UIAlertAction *cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction* action){
[actionSheet dismissViewControllerAnimated:YES completion:nil];
}];
[actionSheet addAction:cancel];
[self presentViewController:actionSheet animated:YES completion:nil];
}
do like change the alert style:UIAlertActionStyleDestructive
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 *resetAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Reset", #"Reset action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
NSLog(#"Reset action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"OK", #"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"OK action");
}];
[alertController addAction:cancelAction];
[alertController addAction:resetAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];

Add custom view in UIAlertController in iOS

I have used below code to show action sheet with Tweet, Facebook and Cancel button.
- (void)shareApp:(id)sender {
NSString *strCancel = NSLocalizedString(#"Cancel", nil);
NSString *strTweet = NSLocalizedString(#"Tweet", nil);
NSString *strFacebook = NSLocalizedString(#"Facebook", nil);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(#"Share your app", nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
// Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:strCancel style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
NSLog(#"Cancel action occured");
}];
UIAlertAction *tweetAction = [UIAlertAction actionWithTitle:strTweet style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"Tweet action here");
}];
UIAlertAction *facebookAction = [UIAlertAction actionWithTitle:strFacebook style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"Facebook action here");
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:tweetAction];
[alertController addAction:facebookAction];
[self presentViewController:alertController animated:YES completion:nil];
}
Now, I want to add custom view i.e. logo + tweet on each element of action sheet.
How can this be implemented?
I got the solution through your code.Just refer the below coding
NSString *strCancel = NSLocalizedString(#"Cancel", nil);
NSString *strTweet = NSLocalizedString(#"Tweet", nil);
NSString *strFacebook = NSLocalizedString(#"Facebook", nil);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(#"Share your app", nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:strCancel style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
NSLog(#"Cancel action occured");
}];
UIAlertAction *tweetAction = [UIAlertAction actionWithTitle:strTweet style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"Tweet action here");
}];
UIAlertAction *facebookAction = [UIAlertAction actionWithTitle:strFacebook style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"Facebook action here");
}];
UIImage *accessoryImage = [UIImage imageNamed:#"Twitter.jpg"];
[tweetAction setValue:accessoryImage forKey:#"image"];
UIImage *accessoryFBImage = [UIImage imageNamed:#"Facebook.png"];
[facebookAction setValue:accessoryFBImage forKey:#"image"];
[alertController addAction:tweetAction];
[alertController addAction:facebookAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES
completion:nil];

UIActivityViewController completion handler shows 'completed' even if user wasn't able to log into Tumblr or other app

I'm using completionWithItemsHandler to determine if selectedItems were posted to Tumblr. I want to present a 'Success!' alert if the post was complete but completionHandler returns 'completed' even if the user wasn't able to log into Tumblr. That is,user goes through the steps of selecting items, opening activity view, and selecting Tumblr --> this opens the Tumblr app and asks for a log in. If the user presses 'cancel' and goes back to my app, the completionHandler says the activity was 'completed'. Is there any way to determine if post was successful? Any way to test if the user was able to log into the app? Any suggestions for dealing with 'success' alerts when there can be these types of problems?
Here is the completion Handler
activityController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *error){
if (error)
{
//NSError *error;
alertController = [UIAlertController alertControllerWithTitle:#"Error" message:#"Error sharing items" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *errorAction = [UIAlertAction actionWithTitle:NSLocalizedString(#"OK", #"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
NSLog(#"Error sharing items: %#", error);
}];
[alertController addAction:errorAction];
[self presentViewController:alertController animated:YES completion:nil];
}
else if (completed)
{
[self.activityIndicator stopAnimating];
if ([self.giftID isEqualToString:#"general"]||[self.giftEntity.giftThanked isEqualToString:#"YES"])
{
alertController = [UIAlertController alertControllerWithTitle:alertTitle message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(#"OK", #"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
// NSLog(#"OK action");
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
if (![self.giftID isEqualToString:#"general"]&&[self.giftEntity.giftThanked isEqualToString:#"NO"])
{
alertController = [UIAlertController alertControllerWithTitle:alertTitle message:#"Mark gift as 'Thanked'?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *yesAction = [UIAlertAction actionWithTitle:NSLocalizedString(#"YES", #"yes action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
self.giftEntity.giftThanked = #"YES";
[self.giftBusObj saveEntities];
[self dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction *noAction = [UIAlertAction actionWithTitle:NSLocalizedString(#"NO", #"no action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
self.giftEntity.giftThanked = #"NO";
[self.giftBusObj saveEntities];
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:noAction];
[alertController addAction:yesAction];
[self presentViewController:alertController animated:YES completion:nil];
}
//Deselect all items in stvc
self.textShare = 0;
self.cardButton.selected = NO;
self.textImageBackground.backgroundColor = [UIColor clearColor];
self.cardImageBackground.backgroundColor = [UIColor clearColor];
[spcvc selectPhotosNone];
[self viewDidLoad];
}
else
{
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(#"activity controller - not 'completed' nor 'error'" );
}
};

Change Action sheet popover arrow in iOS8

i'm using UIAlertController . But on iPad with iOS 8, actionSheet show with popover arrow. Any ideas to hide that arrow?
Here is my code:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"this is alert controller" message:#"yeah" preferredStyle:UIAlertControllerStyleActionSheet];
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");
}];
UIAlertAction *deleteAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Delete", #"Delete action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
NSLog(#"Delete action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[alertController addAction:deleteAction];
UIPopoverPresentationController *popover = alertController.popoverPresentationController;
if (popover) {
popover.sourceView = self.view;
popover.sourceRect = self.view.bounds;
popover.permittedArrowDirections = UIPopoverArrowDirectionUnknown;
}
[self presentViewController:alertController animated:YES completion:nil];
Solution :
use below line for remove arrow from action sheet
[yourAlertController.popoverPresentationController setPermittedArrowDirections:0];
Sample
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Test Action Sheet" message:#"Message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:#"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"OK action");
}];
UIAlertAction *otherAction = [UIAlertAction
actionWithTitle:#"Other"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"Otheraction");
}];
[alertController addAction:okAction];
[alertController addAction:otherAction];
[alertController addAction:cancelAction];
// Remove arrow from action sheet.
[alertController.popoverPresentationController setPermittedArrowDirections:0];
//For set action sheet to middle of view.
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.sourceRect = self.view.bounds;
[self presentViewController:alertController animated:YES completion:nil];
Output
Jageen's answer, in Swift:
popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
The selected answer does not center the alert if you have a nav/status bar. To exactly center your alert controller:
alertController.popoverPresentationController.sourceRect = [self sourceRectForCenteredAlertController];
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.permittedArrowDirections = 0;
With the convenience method:
- (CGRect)sourceRectForCenteredAlertController
{
CGRect sourceRect = CGRectZero;
sourceRect.origin.x = CGRectGetMidX(self.view.bounds)-self.view.frame.origin.x/2.0;
sourceRect.origin.y = CGRectGetMidY(self.view.bounds)-self.view.frame.origin.y/2.0;
return sourceRect;
}
Also, the alert controller does not stay centered if the view is rotated. To keep the alert controller centered you need to update the sourceRect after rotation. For example:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// Check if your alert controller is still being presented
if (alertController.presentingViewController) {
alertController.popoverPresentationController.sourceRect = [self sourceRectForCenteredAlertController];
}
}
Or, if you do not want to use rotation events, you can use the popoverPresentationController delegate method to reposition the popover:
- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing _Nonnull *)view
{
// Re-center with new rect
}
You're on the wrong track using UIPopoverPresentationController to display an alert. You just do not need that snipped of code...
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"this is alert controller" message:#"yeah" preferredStyle:UIAlertControllerStyleActionSheet];
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");
}];
UIAlertAction *deleteAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Delete", #"Delete action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
NSLog(#"Delete action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[alertController addAction:deleteAction];
[self presentViewController:alertController animated:YES completion:nil];

Resources