簡體   English   中英

執行函數以從另一個類調用performSegueWithIdentifier

[英]Execute function to call performSegueWithIdentifier from another class

我已經花了幾個小時試圖自己解決這個問題,但是我放棄了!

我有一個主從布局,其中用戶輸入屏幕需要調用另一個類上的函數以發布到Web服務。 異步調用完成后,該類將調用指定的函數。 在這種情況下,我只是測試,我要做的就是在Web服務接受用戶輸入后返回主屏幕。

當用戶點擊輸入屏幕上的按鈕(SetLocationViewController)時,將在APIPostClass類中調用異步操作。 完成后,我想讓SetLocationViewController選回到MasterViewController。

在APIPostClass.m中(在異步操作完成后調用)

-(void)callWhenDone {
NSLog(@"callWhenDone loaded.");

SetLocationViewController *SLVClassInstance = [[SetLocationViewController alloc] init];
[SLVClassInstance doSegue];
}

在SetLocationViewController.m中

-(void) doSegue {
NSLog(@"doSegue loaded");
[self performSegueWithIdentifier:@"SetLocationViewControllerManualUnwind" sender:self];
}

從SetLocationViewController.m上的操作調用doSegue確實可以,所以我知道我的命令是可以的,但是以上方法不起作用。 我收到錯誤原因:'Receiver()與標識符'SetLocationViewControllerManualUnwind'沒有關聯

我猜測原因是由於VC初始化的alloc init方式,但是我不知道有什么更好的方法。 因此,如何在另一個類上調用函數,就像它是由自己的類調用一樣?

創建一個委托,它將比通知更加可靠和快捷。

@protocol APIPostDelegate <NSObject>
@required
-(void)OnRequestSucess;
@end

在您的APIPost中添加代表的新屬性

@interface APIPost : NSObject

@property (weak) id<APIPostDelegate> delegate;

在SetLocationViewController中實現APIPostDelegate

SetLocationViewController.h

SetLocationViewController :NSObject<APIPostDelegate>

SetLocationViewController.m

-(void)OnRequestSucess
{
   [self doSegue];
}

在APIPost上調用方法之前,請將self分配給委托屬性。

APIPost *apipost=[[APIPost alloc]init];
apipost.delegate=self;

[apipost <your api method>];

APIPost.m

[self.delegate OnRequestSucess];

希望這可以幫助。

有幾種方法可以實現:-

  1. 使用Delegate
  2. 使用NSNotification
  3. 上面Artur所描述的方式(僅適用於SplitViewController -iPad)

您應該盡可能使用委托,但可能不會太直接。 NSNotification更直接,但這不是一個好習慣,也不是一個好的編程風格。

我將只分享NSNotification方法,因為它更易於實現。

SetLocationViewController.m中

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSegue) name:@"calldoSegue" object:nil];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"calldoSegue" object:nil];
}

-(void) doSegue {
    NSLog(@"doSegue loaded");
    [self performSegueWithIdentifier:@"SetLocationViewControllerManualUnwind" sender:self];
}

APIPostClass.m中

-(void)callWhenDone {
    NSLog(@"callWhenDone loaded.");
    [[NSNotificationCenter defaultCenter]postNotificationName:@"calldoSegue" object:nil];
}

上面的代碼應該可以正常工作,但這不是一個好習慣。 您應該嘗試學習Delegate方法。

答案就在這里: 從另一堂課開始表演

在我的APIPostClass.h中,我設置了視圖控制器:

@interface APIPostClass : NSObject  {
    SetLocationViewController *setLocationViewController;
}

@property(nonatomic, strong) SetLocationViewController *setLocationViewController;

@end

在我的APIPostClass.m中,我將其合成:

@synthesize setLocationViewController;

然后,代替這個(就像我的問題一樣):

-(void)callWhenDone {
NSLog(@"callWhenDone loaded.");

SetLocationViewController *SLVClassInstance = [[SetLocationViewController alloc] init];
[SLVClassInstance doSegue];
}

我有:

-(void)callWhenDone {
    NSLog(@"callWhenDone loaded");
    [self.setLocationViewController doSegue];
}

在SetLocationViewController.m中,segue方法保持不變:

-(void) doSegue {
NSLog(@"doSegue loaded");
[self performSegueWithIdentifier:@"SetLocationViewControllerManualUnwind" sender:self];
}

但是,當我調用我的API時,我需要“附加”(原諒我的術語)視圖控制器。 這就是我所擁有的:

- (IBAction)btnTestAPICall:(id)sender {
    NSLog(@"User tapped API button");

    APIPostClass *APIPostClassInstance = [[APIPostClass alloc] init];
    [APIPostClassInstance APICall: ... ....
}

但這是在滿足以上所有條件后起作用的:

- (IBAction)btnTestAPICall:(id)sender {
    NSLog(@"User tapped API button");

    APIPostClass *APIPostClassInstance= [[APIPostClass alloc] init];

    UIViewController *currentVC=self;
    APIPostClassInstance.setLocationViewController = currentVC;

    [APIPostClassInstance APICall: ... ...

我希望這會幫助別人!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM