1

所以我在解决以下问题时遇到了一些棘手的问题。我可以performSegueWithIdentifier使用以下代码在视图控制器之间跳过:

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

但是,我有一个单独的模型类,它承载一长串方法,其中一些我想使用上面的代码。显然,使用“self”不起作用,因为我不再在 viewcontroller 类中。所以我想知道我需要做什么才能修改代码。我最初尝试像这样创建主视图控制器的对象,但这也不起作用(收到错误消息“Receiver () has no segue with identifier '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];
}
4

1 回答 1

3

不要认为您的模型需要呈现视图控制器。将您的模型视为需要获取一些数据。为了获取这些数据,它将向某个对象发送一条消息,请求它需要的特定数据,然后模型将收到一条提供数据的消息。

因此,首先,定义一个协议,其中包含模型需要发送以请求数据的消息。例如:

@protocol PlayerDelegate

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

@end

然后给模型对象一个delegate属性来引用它将向其发送这些请求的对象:

@interface Player (NSObject)

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

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

@end

然后您的播放器视图控制器可以将自己设置为模型的委托并实现该playerNeedsCardFromOpponent:方法以执行适当的转场。当对手选择卡片的视图控制器完成时,它可以通过发送卡片将卡片直接交还给Player(如果它有对 的引用PlayerreceiveCardFromOpponent:,或者它可以将卡片传回给玩家视图控制器,然后发送它到Player.

于 2013-07-05T04:19:46.447 回答