2014-02-21 22 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