如何在NSObject类中显示UIAlertView?

时间:2013-09-20 10:54:58

标签: ios uialertview nsobject

嗨我想在我的UIAlertViews课程中展示一些NSObject。我只是像这样实现正常的方式

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

但这永远不会给我一个提醒。这是为什么?以及如何以正确的方式实现它?

4 个答案:

答案 0 :(得分:2)

如果您没有View Controller,如何使用UIAlertController呈现警报视图。 Description

是的,您只能在UIViewController类中使用UIAlertController。那么我们怎样才能在NSObject类中完成它。如果您看到上面给出的描述链接,您将得到答案。要总结以上描述:在当前窗口上方创建一个新窗口。这个新窗口将是我们显示警报的viewController。 因此,使用此viewController,您可以调用方法[presentViewController: animated: completion:]

<强>答案:

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

                });

答案 1 :(得分:1)

必须从主线程操作

UIKit个元素。如果您的函数是从其他某个线程执行的,则警报可能无法显示。

试试这个,在NSObject班写一个方法,

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

然后当你需要调用它时,请这样调用它,

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

这将在主线程上执行NSObject方法,从而显示警报视图。

希望有所帮助!

答案 2 :(得分:0)

看起来您没有从主线程执行该代码,这意味着一个简单的修复方法是重定向主线程上的[alertFriendsPlaylist show];。试试吧:

dispatch_async(dispatch_get_main_queue(), ^{ 
   [alertFriendsPlaylist show];
});

答案 3 :(得分:-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

我收到提醒 enter image description here