簡體   English   中英

如何從模型類中使用performSegueWithIdentifier?

[英]How to use performSegueWithIdentifier from a model class?

所以我在解決以下問題時遇到了一些棘手的問題。 我可以使用performSegueWithIdentifier在視圖控制器之間跳過使用代碼,如下所示:

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

但是,我有一個單獨的模型類,它包含一長串方法,其中一些我想使用上面的代碼。 顯然,使用“self”不起作用,因為我不再使用viewcontroller類。 所以我想知道我需要做些什么才能修改代碼。 我最初嘗試像這樣創建一個主視圖控制器的對象但是也沒有工作(收到錯誤說“Receiver()沒有帶標識符的segue toOppHand'”):

OneViewController* view=[[OnePlayerViewController alloc]init];
[view performSegueWithIdentifier:@"toOppHand" sender:self];

任何幫助都會非常感激。

編輯:這是我的程序看起來更詳細的視圖。 編輯2:這是我的編輯與下面建議的答案。

型號類:

#import "OnePlayerView Controller"

@protocol PlayerDelegate
//I have an error here "expected a type".
- (void)playerNeedsCardFromOpponent:(OnePlayerViewController *)player;

@end

@interface model : NSObject


@property (nonatomic, weak) id<PlayerDelegate> delegate;


-(void)playBill:(NSString*)cardName{
    //modifying name so it works with the method (make the below easier)
    NSString* newName=[cardName stringByReplacingOccurrencesOfString:@" " withString:@""];
    newName=[newName stringByReplacingOccurrencesOfString:@"?" withString:@""];
    newName=[newName stringByReplacingOccurrencesOfString:@"," withString:@""];
    newName=[newName stringByReplacingOccurrencesOfString:@"!" withString:@""];
    newName=[newName stringByReplacingOccurrencesOfString:@";" withString:@""];
    newName=[newName stringByReplacingOccurrencesOfString:@"." withString:@""];
    newName=[newName stringByReplacingOccurrencesOfString:@"'" withString:@""];
    [self performSelector:NSSelectorFromString(newName)];
}

-(void)CardType1{

    OnePlayerViewController* view=[[OnePlayerViewController alloc]init];
    [view performSegueWithIdentifier: @"toOppHand" sender: self];    

}

查看控制器:

- (IBAction)playCard:(id)sender {
    int cellNumber=self.SelectedRowPointer.row;
    NSString* cardType=[[self.CardsinHandPointer.player1_hand objectAtIndex:cellNumber]cardType];
    NSString* cardName=[[self.CardsinHandPointer.player1_hand objectAtIndex:cellNumber]cardName];
    if ([cardType isEqualToString:@"bill"]){
        [self.CardsinHandPointer addCardtoPortfolio:cellNumber forPlayer:1];
        [self.CardsinHandPointer playBill:cardName];
    }
    if ([cardType isEqualToString:@"location"]){
        [self.CardsinHandPointer playLocations:cellNumber forPlayer:1];
    }
    if ([cardType isEqualToString:@"personality"]){
        [self.CardsinHandPointer playPersonalities:cellNumber forPlayer:1];
    }

    [self.navigationController popToRootViewControllerAnimated:YES];
}

不要認為您的模型需要呈現視圖控制器。 將您的模型視為需要獲取一些數據。 要獲取該數據,它將向某個對象發送消息,請求所需的特定數據,稍后該模型將收到提供數據的消息。

首先,定義一個協議,其中包含模型需要發送的消息或消息以請求數據。 例如:

@protocol PlayerDelegate

- (void)playerNeedsCardFromOpponent:(Player *)player;

@end

然后給模型對象一個delegate屬性來引用它將發送這些請求的對象:

@interface Player (NSObject)

@property (nonatomic, weak) id<PlayerDelegate> delegate;

- (void)receiveCardFromOpponent:(Card *)card;

@end

然后你的播放器視圖控制器可以將自己設置為模型的委托,並實現playerNeedsCardFromOpponent:方法來執行適當的segue。 當對手牌選擇視圖控制器完成時,它可以通過發送它的receiveCardFromOpponent:將卡直接交回給Player (如果它有對Player的引用),或者它可以將卡傳遞回玩家視圖控制器然后將其發送給Player

暫無
暫無

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

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