2014-09-02 35 views
0

我知道這是雙重問題。我知道我可以使用toViewController中的屬性來獲取UIViewController的名稱來獲取NSString,它告訴我我來自哪裏。在iOS中如何從ViewController名稱獲取對象?

無論如何,我想問是否有一個簡單的方法來獲得UIViewControllerunwindingsegue名稱。

我有一個UIViewControllersegues 3種形式。我以編程方式返回到該視圖控制器。只有當我從視圖控制器返回時才需要運行特定的代碼。我的目標是從名稱fromViewController開始使用字符串的特定代碼。

回答

1

使用UIViewController通過NSString從它的類名是不夠安全,因爲該名稱可以更改。 您可以使用isKindOfClass代替:

 UIViewController *destinationViewController = segue.destinationViewController; 
     if ([destinationViewController isKindOfClass:[MyViewControllerClass1 class]]) { 
     // put code related to transition to MyViewControllerClass1 
     } 
     else if ([destinationViewController isKindOfClass:[MyViewControllerClass2 class]]) { 
     // put code related to transition to MyViewControllerClass2 
     } 
+0

10x這是我需要的。 – new2ios 2014-09-02 12:46:20

0

您可以使用:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    UIViewController *destinationViewController = segue.destinationViewController; 
    NSString * identifier = destinationViewController.identifier; 
    NSString * title = destinationViewController.title; 
} 
0

在主VC創建自定義的委託方法,創建3個字符串具有獨特的名字好讓你能識別。 EG。

NSString* stringFrmFORM1, *stringFrmFORM2, *stringFrmFORM3; 
-(void)setString:(NSString*)myString{ 
    //set the string from the VC1,2,3 to each string based on Primary VC's strings 
} 

從每個註冊VC調用委託方法,並設置這些字符串。

您將從註冊VC的每個註冊字符串中爲每個您設置的唯一字符串註冊字符串。

+0

該解決方案是正確的,但我實際上不想爲視圖控制器添加其他屬性。我想使用unwind segue返回的對象並使用它來知道我來自哪裏。對不起,但我的名聲不允許我投你的答案。 – new2ios 2014-09-02 12:48:08

+0

沒問題:)但請確保你不會讓你的循環循環,這將使VC對象重疊並保持增加內存。 當你從下一個Segue返回到前一個VC時使用委託。 乾杯:) – 2014-09-02 12:49:39

0

要回答你的基地問題,你可以以字符串形式獲取類的名字與:

NSString *strClassName = NSStringFromClass([fromViewController class]); 

但@AlexPeda中澤回答指​​出,-isKindOfClass:會更好。

if ([fromViewController isKindOfClass:[SpecificViewController class]]) {  
    //run your 'specific' code 
}