所以我在解决以下问题时遇到了一些棘手的问题。我可以使用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];
}
答案 0 :(得分:3)
不要将您的模型视为需要呈现视图控制器。将您的模型视为需要获取一些数据。要获取该数据,它将向某个对象发送消息,请求所需的特定数据,稍后模型将收到提供数据的消息。
首先,定义一个协议,其中包含模型需要发送的消息以请求数据。例如:
@protocol PlayerDelegate
- (void)playerNeedsCardFromOpponent:(Player *)player;
@end
然后给模型对象一个delegate
属性来引用它将发送这些请求的对象:
@interface Player (NSObject)
@property (nonatomic, weak) id<PlayerDelegate> delegate;
- (void)receiveCardFromOpponent:(Card *)card;
@end
然后您的播放器视图控制器可以将自己设置为模型的委托,并实现playerNeedsCardFromOpponent:
方法来执行适当的segue。当对手牌选择视图控制器完成时,它可以通过发送它Player
直接将卡片交回Player
(如果它具有对receiveCardFromOpponent:
的引用),或者它可以将卡片传回播放器视图控制器,然后将其发送到Player
。