3

嗨,我想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

4 回答 4

2

没有视图控制器时如何使用 UIAlertController 呈现警报视图。说明

是的,您只能在 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];

                });
于 2016-04-04T06:23:03.530 回答
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这将在主线程上执行该方法,从而显示警报视图。

希望有帮助!

于 2013-09-20T11:16:49.720 回答
0

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

dispatch_async(dispatch_get_main_queue(), ^{ 
   [alertFriendsPlaylist show];
});
于 2013-09-20T11:16:53.773 回答
-1

视图控制器.h

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

视图控制器.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

我收到了警报在此处输入图像描述

于 2013-09-20T11:22:56.000 回答