2014-02-21 20 views
0

我是IOS开发新手。我正在尝试在其他课程上执行ViewController1的继续练习,并且我正在使用NSNotificationCenter将segue方法访问到其他课程。该SEGUE工作,让我的无法在iOS 5的其他课程中执行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当我叫它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]; 
} 

回答

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()包裹,^ {},但我相信你没有做任何UI相关的东西在你的NSObject类。

还要注意开卷塞格斯我相信iOS6是新的,因此对于iOS5,您需要执行常规的游戏或以代码方式呈现新的视图控制器。

1

你不应该访问VC1的SEGUE从VC2“VC2”这样的。如果您在UINavigationController下,那么在呈现VC2时,会在左侧会自动添加一个Back按钮,以便您将用户返回到VC1。

如果你不在NC下,或者你想完全控制下一个segue,你可以使用VC2中的unwind segue来选择你想要的VC。如果你想回去VC1,而不由上述NC提供的选项,你只需将此代码添加到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从VC2到它的退出(上最好的实现了新的SEGUE在IB窗口中的VC窗口中,只需控制 - 从黄色VC图标拖动到绿色的Exit图标)。您将看到放松的赛车选项,您只需选择您在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 
+0

我需要在NSObject类中调用segue操作而不是在控制器上 – NewDroidDev

+0

如果NSObject类是你模型的一部分,那么你不应该从模型中调用segues。控制器在这里管理视图a在他们之间徘徊。如果您对其他类执行segue,则其他类应该是UIViewController或它的一个子类。你的NSObject是什么类? –

+0

你是什么意思什么样的类是我的NSObject?我创建了一个类型为NSObject的类,并在循环结束后声明了循环内部的函数。我想要转到另一个控制器,这就是为什么我要在自定义类中调用segue而不是在控制器中。 – NewDroidDev