Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在NSObject类中显示UIAlertView?_Ios_Uialertview_Nsobject - Fatal编程技术网

Ios 如何在NSObject类中显示UIAlertView?

Ios 如何在NSObject类中显示UIAlertView?,ios,uialertview,nsobject,Ios,Uialertview,Nsobject,您好,我想在我的NSObject类中显示一些UIAlertView。我只是按照这样的正常方式来实现 if (data != nil) { @try { NSDictionary *result=[data JSONValue]; if ([[result valueForKey:@"success"] integerValue]==1) { NSMutableArray *friendsPlaylistArray=[result

您好,我想在我的
NSObject
类中显示一些
UIAlertView
。我只是按照这样的正常方式来实现

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

但这从来没有给我一个警告。为什么呢?以及如何以正确的方式实现它?

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
方法,从而显示警报视图


希望有帮助

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

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

viewcontrollor.h

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

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

我收到了警报

当您没有视图控制器时,如何使用UIAlertController显示警报视图

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

回答:

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

                });

它不会给出任何错误或崩溃。只是不显示警报视图这是一个NSObject类。不是视图控制器。在NSObject类中,我尝试显示警报视图我尝试显示来自简单NSObject类的警报,它正确显示了警报。我相信您的情况可能没有到达else部分。但它会执行警报代码。但不显示警报。