gpt4 book ai didi

iphone - 单击 iPhone 上的按钮时触发其他类的方法

转载 作者:搜寻专家 更新时间:2023-10-30 20:21:05 25 4
gpt4 key购买 nike

我刚刚制作了一个“应用程序”,它只是在命令行(outpot)中打印一些东西这是一个例子 -

http://img254.imageshack.us/img254/2853/3cc0a810d7564e39b05bade.png

代码:

卡片.h:

#import <Foundation/Foundation.h>
@interface Card : NSObject {
int CardValue;
int CardType;
}
@property int CardValue,CardType;
@end

卡片.m:

#import <Foundation/Foundation.h>
@interface Card : NSObject {
int CardValue;
int CardType;
}
@property int CardValue,CardType;
@end

Deck.h:

#import <Foundation/Foundation.h>
#import "Card.h"
@interface Deck : NSObject {
NSMutableArray *Deck1, *Deck2, *Deck3, *Deck4, *Deck5;
}
@property NSMutableArray *Deck1, *Deck2, *Deck3, *Deck4, *Deck5;
-(void)createDeck;
-(void)createFullDeck : (Deck *) TheDeck;
-(void)insertToDeck : (Card *) TheCard;
@end

甲板.m:

#import "Deck.h"
#include <stdlib.h>
@implementation Deck
@synthesize Deck1, Deck2, Deck3, Deck4, Deck5;
-(void)createDeck{
Deck1 = [NSMutableArray arrayWithCapacity:13];
Deck2 = [NSMutableArray arrayWithCapacity:13];
Deck3 = [NSMutableArray arrayWithCapacity:13];
Deck4 = [NSMutableArray arrayWithCapacity:13];
Deck5 = [NSMutableArray arrayWithCapacity:13];
}
-(void)createFullDeck : (Deck *) TheDeck{
Card *Card1 = [[Card alloc]init];
[TheDeck createDeck];
for (int i=1;i<=4;i++){
for (int j=1;j<=13;j++){
Card1.CardValue=j;
Card1.CardType=i;
[TheDeck insertToDeck:Card1];
}
}
}
-(void)insertToDeck : (Card *) TheCard{
if (TheCard.CardType==1)
[Deck1 addObject:[NSNumber numberWithInteger:TheCard.CardValue]];
if (TheCard.CardType==2)
[Deck2 addObject:[NSNumber numberWithInteger:TheCard.CardValue]];
if (TheCard.CardType==3)
[Deck3 addObject:[NSNumber numberWithInteger:TheCard.CardValue]];
if (TheCard.CardType==4)
[Deck4 addObject:[NSNumber numberWithInteger:TheCard.CardValue]];
}
-(id)DrawCard{
Card *MethodCard = [[Card alloc]init];
int randomType = arc4random() % 4;
if (randomType==0){
int DeckCount = [Deck1 count];
int randomValue = arc4random() % DeckCount;
MethodCard.CardValue = [[Deck1 objectAtIndex:randomValue]integerValue];
[Deck1 removeObjectAtIndex:randomValue];
}
if (randomType==1){
int DeckCount = [Deck2 count];
int randomValue = arc4random() % DeckCount;
MethodCard.CardValue =[[Deck2 objectAtIndex:randomValue]integerValue];
[Deck2 removeObjectAtIndex:randomValue];
}
if (randomType==2){
int DeckCount = [Deck3 count];
int randomValue = arc4random() % DeckCount;
MethodCard.CardValue =[[Deck3 objectAtIndex:randomValue]integerValue];
[Deck3 removeObjectAtIndex:randomValue];
}
if (randomType==3){
int DeckCount = [Deck4 count];
int randomValue = arc4random() % DeckCount;
MethodCard.CardValue =[[Deck4 objectAtIndex:randomValue]integerValue];
[Deck4 removeObjectAtIndex:randomValue];
}
MethodCard.CardType = randomType+1;
return MethodCard;
}
@end

游戏.h:

#import <Foundation/Foundation.h>
#import "Deck.h"
@interface Game : NSObject

-(void)StartNewGame : (Deck *) TheDeck : (Game *) TheGame;
-(BOOL)GetDecision : (int) TheNumber;
-(void)CheckHiOrLo : (int) FirstNumber : (Deck *) TheDeck : (Game *) TheGame;
@end

游戏.m:

#import "Game.h"
@implementation Game
-(void)StartNewGame : TheDeck : (Game *) TheGame{
NSLog(@"Welcome to the game High-Low!");
[TheDeck createDeck];
[TheDeck createFullDeck:TheDeck];
Card *TheCard = [[Card alloc]init];
TheCard = [TheDeck DrawCard];
[TheGame CheckHiOrLo: TheCard.CardValue : TheDeck : TheGame];
}
-(BOOL)GetDecision : (int) TheNumber{
int userNum=0;
NSLog(@"The Card is %i\nif you think the next Card is going to be higher - type 1.\nif you think the next Card is going to be lower - type 0.", TheNumber);
scanf("%i", &userNum);
if(userNum==1)
return YES;
if(userNum==0)
return NO;
NSLog(@"error in GetDecision in Game.m");
return NO;
}
-(void)CheckHiOrLo : (int) FirstNumber : (Deck *) TheDeck : (Game *) TheGame{
Card *TheCard = [[Card alloc]init];
TheCard = [TheDeck DrawCard];
BOOL Decision = [TheGame GetDecision:FirstNumber];
if(FirstNumber>TheCard.CardValue){
if(Decision==YES){
NSLog(@"%i", TheCard.CardValue);
NSLog(@"GameOver");
}
if(Decision==NO){
NSLog(@"%i \nCorrect, Next Card", TheCard.CardValue);
[TheGame CheckHiOrLo: TheCard.CardValue : TheDeck : TheGame];
}
}
if(FirstNumber<=TheCard.CardValue){
if(Decision==YES){
NSLog(@"%i \nCorrect, Next Card", TheCard.CardValue);
[TheGame CheckHiOrLo: TheCard.CardValue : TheDeck : TheGame];
}
if(Decision==NO){
NSLog(@"%i", TheCard.CardValue);
NSLog(@"GameOver");
}
}
}
@end

所以现在我尝试为此制作一个 iPhone 应用程序,所以我制作了一个新的 iPhone 项目,将 Card、Deck 和游戏类移至 iPhone 项目,并制作了一个高按钮、一个低按钮和一个开始按钮建立一个新的平台。

现在我该怎么做,当我点击开始按钮时,它会触发 Deck 类中的方法 createDeck 和 CreateFullDeck?

最佳答案

首先你要在你的头文件中使 Deck 成为声明的对象,比方说你的 UIViewController,确保你 #import Deck.h.

#import "Deck.h"

@interface MainViewController: UIViewController

Deck *deck;
@end

现在,您可以在 MainViewController 的 -viewDidLoad 方法中初始化您的 deck

-(void)viewDidLoad {

[super viewDidLoad];

deck = [[Deck alloc] init];
}

在您的按钮操作中,您将创建完整的牌组。假设这是一个连接到 Interface Builder 中的 UIButtonIBAction

 -(IBAction)onCreateClicked:(id)sender {

[deck createDeck:deck];
// You now created your deck of cards.
}

希望对您有所帮助!

编辑:在进一步调查您的代码后,您的代码有很多问题,但上面的示例将按照您的要求执行。

关于iphone - 单击 iPhone 上的按钮时触发其他类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10950874/

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