iPhone開発-コード表示コントロール


今までずっとXibファイルドラッグコントロールで勉强していましたが、今日は初めてコードだけでインタフェースを描きました.小さなプログラムですが、初心者の私にとっては一日研究しました.やっと始まった!
.hファイル
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController
{
    UIImageView *imageView;
}
@property (nonatomic,retain) UIImageView *imageView;
@end

.mファイル
//
//  RootViewController.m
//  noxib
//
//  Created by J plus on 7/31/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController
@synthesize imageView;

-(void)dealloc
{
    [imageView release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *view = 
    [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; 
    
    
    //      :   
    view.backgroundColor = [UIColor lightGrayColor]; 
    //      
    
    CGRect frame=CGRectMake(0, 0, 160, 230);
    imageView=[[UIImageView alloc] initWithFrame:frame];
    imageView.image=[UIImage imageNamed:@"1.jpeg"];
    
    imageView.tag=1001;
    [view addSubview:imageView];
    self.view=view;
    
    
    frame = CGRectMake(10, 265, 300, 20);//          x y width height          
    UILabel *label = [[UILabel alloc] initWithFrame:frame]; //        ,      frame  
    label.textAlignment = UITextAlignmentCenter; //               
    label.backgroundColor = [UIColor clearColor]; //           
    label.font = [UIFont fontWithName:@"Verdana" size:20]; //     Verdana 20     
    label.text = @"This is a label1";//      
    label.tag = 1000; 
    [view addSubview:label];
    
    
    //  text 
    frame = CGRectMake(10, 320, 300, 30); 
    UITextField *text=[[UITextField alloc]initWithFrame:frame]; 
    text.backgroundColor=[UIColor whiteColor]; 
    text.text=@"I am Roger!!"; 
    [view addSubview:text];
    //      
    
    
    frame= CGRectMake(10, 370, 300, 50);//        
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = frame; 
    [button setTitle:@"Click Me, Please!" forState:UIControlStateNormal]; 
    button.backgroundColor = [UIColor clearColor]; 
    button.tag = 2000; 
    [button addTarget:self 
               action:@selector(buttonClicked:) //      
     forControlEvents:UIControlEventTouchUpInside]; 
    //   ,        view      
    [view addSubview:button]; 
     
    self.view = view;   
    //      UIView  ,         ,UIScreen mainScreen        ,applicationFrame           
    
    
    
	// Do any additional setup after loading the view, typically from a nib.
}

-(IBAction) buttonClicked: (id) sender{
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Action invoked!" message:@"Button clicked!"
                          delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end