所以我在解决以下问题时遇到了一些棘手的问题。我可以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];
}