#import "GameViewController.h"
@interface GameViewController ()
@end
@implementation GameViewController
- (void) dealloc
{
[_nameLabel release];
_nameLabel = nil;
[super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_nameLabel =[[UILabel alloc] initWithFrame:CGRectMake(60, 20, 200, 40)];
[_nameLabel setBackgroundColor:[UIColor whiteColor]];
[_nameLabel setText:@"白雪公主的黑暗騎士丶"];
UIFont * font = [UIFont boldSystemFontOfSize:20];
[_nameLabel setFont:font];
[_nameLabel setTextAlignment:NSTextAlignmentCenter];
[_nameLabel setTextColor:[UIColor redColor]];
[self.view addSubview:_nameLabel];
[_nameLabel release];
UILabel * ziLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 420, 200, 40)];
[ziLabel setBackgroundColor:[UIColor whiteColor]];
[ziLabel setText:@"小火龍丶就決定是你了"];
UIFont * font1 = [UIFont boldSystemFontOfSize:20];
[ziLabel setFont:font1];
[ziLabel setTextAlignment:NSTextAlignmentCenter];
[ziLabel setTextColor:[UIColor redColor]];
[self.view addSubview:ziLabel];
[ziLabel release];
[self creatImageButton];
}
- (void) creatImageButton
{
int tag = 1;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 6; j++) {
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(10 + 50 * j, 70 + 50 * i, 50, 50)];
[button setImage:[UIImage imageNamed:@"321.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"000.png"] forState:UIControlStateSelected];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:tag++];
[self.view addSubview:button];
[button release];
}
}
}
- (void) buttonAction:(UIButton*)sender
{
NSLog(@"%d",sender.tag);
[sender setSelected:![sender isSelected]];
if ((sender.tag % 6 != 1) ) {
UIButton * abutton = (UIButton*)[self.view viewWithTag:sender.tag-1];
[abutton setSelected:![abutton isSelected]];
}
if ((sender.tag % 6 != 0) ) {
UIButton * abutton = (UIButton*)[self.view viewWithTag:sender.tag+1];
[abutton setSelected:![abutton isSelected]];
}
if (sender.tag > 6) {
UIButton * abutton = (UIButton*)[self.view viewWithTag:sender.tag-6];
[abutton setSelected:![abutton isSelected]];
}
if (sender.tag < 37) {
UIButton * abutton = (UIButton*)[self.view viewWithTag:sender.tag+6];
[abutton setSelected:![abutton isSelected]];
}
}
---------------------------------分割線---------------------------------------
GameViewController.h
#import <UIKit/UIKit.h>
@interface GameViewController : UIViewController
//顯示遊戲名
@property (retain, nonatomic) UILabel * nameLabel;
@end
#import "AppDelegate.h"
#import "GameViewController.h"
@implementation AppDelegate
- (void) dealloc
{
[_window release];
_window = nil;
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
GameViewController * game = [[GameViewController alloc] init];
[self.window setRootViewController:game];
[self.window makeKeyAndVisible];
return YES;
}