gpt4 book ai didi

ios - 如何使用模型类中的 performSegueWithIdentifier?

转载 作者:行者123 更新时间:2023-12-01 19:09:29 26 4
gpt4 key购买 nike

所以我在解决以下问题时遇到了一些棘手的问题。我可以使用 performSegueWithIdentifier使用如下代码在 View Controller 之间跳转:

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

但是,我有一个单独的模型类,它承载一长串方法,其中一些我想使用上面的代码。显然,使用“self”不起作用,因为我不再在 viewcontroller 类中。所以我想知道我需要做什么才能修改代码。我最初尝试像这样创建主视图 Controller 的对象,但这也不起作用(收到错误消息“Receiver () has no segue with identifier 'toOppHand'”):
OneViewController* view=[[OnePlayerViewController alloc]init];
[view performSegueWithIdentifier:@"toOppHand" sender:self];

任何帮助将不胜感激。

编辑:这是我的程序外观的更详细 View 。
编辑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];

}

View Controller :
- (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];
}

最佳答案

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

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

@protocol PlayerDelegate

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

@end

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

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

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

@end

然后您的播放器 View Controller 可以将自己设置为模型的委托(delegate)并实现 playerNeedsCardFromOpponent:方法来执行适当的 segue。当对手卡选择 View Controller 完成时,它可以将卡直接交还给 Player。 (如果它有对 Player 的引用)通过发送 receiveCardFromOpponent: ,或者它可以将卡片传回播放器 View Controller ,然后将其发送到 Player .

关于ios - 如何使用模型类中的 performSegueWithIdentifier?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17480018/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com