使用XIB实现一个简单view

技术处女贴 欢迎来探讨

转帖请注明出处 http://www.cnblogs.com/andy-zhou/p/4962135.html

微信: @Andy

1. AppDelegate

AppDelegate.h 文件

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) UITabBarController *tabBarController;

@end

AppDelegate.m 文件

#import "AppDelegate.h"
#import "ViewController.h"
#import "AZImageViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

//    ViewController *viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
//    self.window.rootViewController = viewController;
    
    ViewController *viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    AZImageViewController *imageViewController = [[[AZImageViewController alloc] initWithNibName:@"AZImageViewController" bundle:nil] autorelease];
    
    self.tabBarController = [[UITabBarController new] autorelease];
    self.tabBarController.viewControllers = @[viewController, imageViewController];
    self.window.rootViewController = self.tabBarController;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

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

@end

2. ViewController

ViewController.h 文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (retain, nonatomic) IBOutlet UILabel *status;


- (IBAction)leftButtonPress:(id)sender;

- (IBAction)rightButtonPress:(id)sender;


- (IBAction)setStatusText:(id)sender;


@end

ViewController.m 文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize status;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"First", @"First");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

- (IBAction)leftButtonPress:(id)sender {
     status.text = @"this is left btn";
}

- (IBAction)rightButtonPress:(id)sender {
    status.text = @"this is right btn";
}

- (IBAction)setStatusText:(id)sender {
    
    NSString *title = [sender titleForState:UIControlStateNormal];
//    status.text = title;
    NSString *newLableText = [NSString stringWithFormat:@"Hello, %@ btn", title];
    status.text = newLableText;
}

@end

ViewController.xib 文件


3. AZImageViewController

AZImageViewController.h 文件

#import "ViewController.h"

@interface AZImageViewController : ViewController

@property (retain, nonatomic) IBOutlet UITextField *nameField;

@property (retain, nonatomic) IBOutlet UITextField *numberField;

@property (retain, nonatomic) IBOutlet UILabel *rateLabel;

- (IBAction)textFieldDoneEditing:(id)sender;

- (IBAction)backgroudTap:(id)sender;

- (IBAction)sliderMove:(id)sender;

@end

AZImageViewController.m 文件

#import "AZImageViewController.h"

@interface AZImageViewController ()

@end

@implementation AZImageViewController

@synthesize nameField;
@synthesize numberField;
@synthesize rateLabel;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Second", @"Second");
        self.tabBarItem.image = [UIImage imageNamed:@"second"];
    }
    return self;
}

#pragma mark - Custom Method

- (IBAction)textFieldDoneEditing:(id)sender {
    [sender resignFirstResponder];
}

- (IBAction)backgroudTap:(id)sender {
    [nameField resignFirstResponder];
    [numberField resignFirstResponder];
}

- (IBAction)sliderMove:(id)sender {
    
    UISlider *slider = (UISlider *)sender;
    NSInteger _rate = (NSInteger)roundf(slider.value);
    rateLabel.text = [NSString stringWithFormat:@"%ld", _rate];
}

- (void)dealloc {
    [nameField release];
    [numberField release];
    [rateLabel release];
    [super dealloc];
}
@end

AZImageViewController.xib 文件


4. info_plist

阿莫斯论Amos

posted @ 2015-11-13 15:39  Andrew.Zhou  阅读(1136)  评论(0编辑  收藏  举报