3

hi I want to show some UIAlertViews within my NSObject class. I just implement the normal way like this

 if (data != nil)
{
    @try {
        NSDictionary *result=[data JSONValue];
        if ([[result valueForKey:@"success"] integerValue]==1) {

            NSMutableArray *friendsPlaylistArray=[result valueForKey:@"comments"];
            return friendsPlaylistArray;
        }
        else
        {
            UIAlertView *alertFriendsPlaylist=[[UIAlertView alloc] initWithTitle:@"Thala Live" message:[[result valueForKey:@"errors"] valueForKey:@"errMessage"] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

            [alertFriendsPlaylist show];
        }

But this is never gives me an alert. Why is that? and how I can implement it in correct way?

Amar
  • 13,202
  • 7
  • 53
  • 71
iDia
  • 1,397
  • 8
  • 25
  • 44
  • it doesnt give any error or crashing. just doesnt show the alert view – iDia Sep 20 '13 at 10:58
  • This is a NSObject class. Not a viewController. within a NSObject class Im try to show an alert view – iDia Sep 20 '13 at 11:02
  • I tried to show an alert from a simple NSObject class, and it showed the alert properly. I believe your condition might not be reaching the else section. – CodenameLambda1 Sep 20 '13 at 11:05
  • but it execute the alert code. but not display the alert. – iDia Sep 20 '13 at 11:11

4 Answers4

2

How To Present An Alert View Using UIAlertController When You Don't Have A View Controller. Description.

Yes, you can only use UIAlertController only in UIViewController classes. So how can we do it in NSObject classes. If you see the description link given above you will get to the answer. To summarise in a line for the above description: Create a new window above the the current window. This new window will be our viewController where we display alert. So using this viewController you can call the method [presentViewController: animated: completion:].

Answer:

dispatch_async(dispatch_get_main_queue(), ^{

                    UIWindow* window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

                    window.rootViewController = [UIViewController new];
                    window.windowLevel = UIWindowLevelAlert + 1;
                    NSString *msg=@“Your mssg";
                    UIAlertController* alertCtrl = [UIAlertController alertControllerWithTitle:@“Title" message:msg preferredStyle:UIAlertControllerStyleAlert];

                    [alertCtrl addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Yes",@"Generic confirm") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

                        // do your stuff
                        // very important to hide the window afterwards.                       
                        window.hidden = YES;

                    }]];

                    UIAlertAction *cancelAction= [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

                         window.hidden = YES; 

                    }];

                    [alertCtrl addAction:cancelAction];

                //http://stackoverflow.com/questions/25260290/makekeywindow-vs-makekeyandvisible

                    [window makeKeyAndVisible]; //The makeKeyAndVisible message makes a window key, and moves it to be in front of any other windows on its level
                    [window.rootViewController presentViewController:alertCtrl animated:YES completion:nil];

                });
Utsav Dusad
  • 2,139
  • 4
  • 31
  • 52
1

UIKit elements must be operated from the main thread. If your function is executed from some other thread, it is a possibility that the alert does not get displayed.

Try this, in your NSObject class write a method,

-(void) showAlert {
    UIAlertView *alertFriendsPlaylist=[[UIAlertView alloc] initWithTitle:@"Thala Live" message:[[result valueForKey:@"errors"] valueForKey:@"errMessage"] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alertFriendsPlaylist show];
}

Then when you need to call this, call it this way,

[self performSelectorOnMainThread:@selector(showAlert) withObject:nil waitUntilDone:NO];

This will execute that NSObject method on main thread, thus showing alert view.

Hope that helps!

Amar
  • 13,202
  • 7
  • 53
  • 71
0

Looks like you're not executing that code from the main thread, which means that an easy fix would be to redirect the [alertFriendsPlaylist show]; on the main thread. Just try with:

dispatch_async(dispatch_get_main_queue(), ^{ 
   [alertFriendsPlaylist show];
});
micantox
  • 5,446
  • 2
  • 23
  • 27
-1

ViewContrllor.h

#import <UIKit/UIKit.h>
#import "ss.h"  // Custom Object file
@interface ViewController : UIViewController
{
    //ss *scv;
}
@property(nonatomic,retain)ss *scv;
@end

ViewController.m

@synthesize scv;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    scv=[[ss alloc]init];
    [scv gt];

}

ss.h

#import <Foundation/Foundation.h>
@class ss;
@interface ss : NSObject
-(void)gt;
@end

ss.m

#import "ss.h"
@implementation ss
-(void)gt
{
    UIAlertView *alertFriendsPlaylist=[[UIAlertView alloc] initWithTitle:@"Thala Live" message:@"GGOD" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

    [alertFriendsPlaylist show];
}
@end

I got the alertenter image description here

user1673099
  • 3,293
  • 7
  • 26
  • 57