在iOS 5中无法在其他类中执行segue?

时间:2014-02-21 04:03:42

标签: ios segue nsnotificationcenter

我是IOS开发的新手。我正在尝试在其他类上执行ViewController1的segue,我正在使用NSNotificationCenter来访问其他类的segue方法。当我在ViewController1内调用它时,segue正在工作,但是当我将它调用到其他类时,它会给我一个错误

NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 
'NSBundle </Users/dev03/Library/Application Support/iPhone Simulator/7.0.3/Applications/9Bsf3724-557D-40F4-A369-F58A31234120/MedsRUs.app> 
(loaded)' with name 'UIViewController-LTT-QY-pu7' and directory 'Main_iPhone.storyboardc

在我的ViewController1中,我有这个:

- (void)viewDidLoad{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goToSelectItem:) name:@"performSegueSoldToToSelectItem" object:nil];

}

-(void)goToSelectItem:(NSNotification *)Notif{

    [self performSegueWithIdentifier:@"soldTotoSelectItem" sender:self];
 }

这就是我在另一个类上调用方法goToSelectItem

-(void)nextController{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"performSegueSoldToToSelectItem" object:nil];
}

2 个答案:

答案 0 :(得分:1)

你不应该像这样从VC2访问VC1的segue到'VC2'。如果您在UINavigationController下,那么在呈现VC2时,左侧会有一个自动添加的Back按钮,将用户带回VC1。

如果您不在NC下或想要完全控制下一个segue,可以使用VC2中的unwind segue到您想要的任何VC。如果您想在没有上述NC提供的选项的情况下返回VC1,只需将此代码添加到VC1:

- (IBAction)UnwindFromVC2:(UIStoryboardSegue *)segue
{
    // can add code to execute after the segue is performed
    // note it must be IBAction and the attribute must be UIStoryboardSegue
}

然后在IB中创建一个新的segue从VC2到它的退出(最好在IB的VC窗口下的栏上实现,只需控制 - 从黄色VC图标拖动到绿色的退出图标)。您将看到unwind segue选项,您只需选择您在VC1中准备的那个。您必须在IB中设置segue的标识符。

然后你从VC2调用它:

[self performSegueWithIdentifier:@"UnwindFromVC2" sender:self];

编辑 - 委托模式示例:

ViewController.m

#import "ViewController.h"
#import "MyNSObject.h"

@interface ViewController () <MyCallBack>

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    MyNSObject *myNSObject = [[MyNSObject alloc] init];

    // set myNSObject to be my delegate
    myNSObject.delegate = self;

    // do something with myNSObject
}

- (void)doSegueForMe
{
    [self performSegueWithIdentifier:@"mySegueIdentifier" sender:self];
}

@end

MyNSObject.h

#import <Foundation/Foundation.h>

@protocol MyCallBack <NSObject>

- (void)doSegueForMe;

@end

@interface MyNSObject : NSObject

@property (nonatomic, strong) id <MyCallBack> delegate;

@end

MyNSObject.m

#import "MyNSObject.h"

@implementation MyNSObject

- (void)loopFinished
{
    [self.delegate doSegueForMe];
}

@end

答案 1 :(得分:0)

另一种选择是使用块和后台线程。 dispatch_async之后的代码块在后台线程上执行,不会阻止UI。代码完成后,您只需执行segue,这次是在主线程上。

#import "ViewController.h"
#import "MyNSObject.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    MyNSObject *myNSObject = [[MyNSObject alloc] init];

    // do something with myNSObject on a background thread

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{

        [myNSObject loopFinished];

        dispatch_async(dispatch_get_main_queue(), ^{

            // perform on main
            [self performSegueWithIdentifier:@"mySegueIdentifier" sender:self];
        });
    });
}

@end

所有UI更新都需要使用dispatch_async(dispatch_get_main_queue(),^ {}包装,但我假设您没有在NSObject类中执行任何与UI相关的操作。

另外请注意unwind segues在iOS6中是新的我相信,所以对于iOS5,您需要执行常规segue或在代码中使用推送或模态呈现新的视图控制器。