protocol在ios中的应用

来源:互联网 发布:方天画戟淘宝 编辑:程序博客网 时间:2024/05/17 03:36
//
//  AppDelegate.h
//  Protocol_demo
//
//  Created by qianfeng on 13-5-23.
//  Copyright (c) 2013qianfeng. All rights reserved.
//


#import <UIKit/UIKit.h>


@class RootViewController;


@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;


@property (strong, nonatomic) RootViewController *viewController;


@end


//
//  AppDelegate.m
//  Protocol_demo
//
//  Created by qianfeng on 13-5-23.
//  Copyright (c) 2013骞?qianfeng. All rights reserved.
//


#import "AppDelegate.h"


#import "RootViewController.h"


@implementation AppDelegate


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


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end



//
//  ViewController.h
//  Protocol_demo
//
//  Created by qianfeng on 13-5-23.
//  Copyright (c) 2013骞?qianfeng. All rights reserved.
//


#import <UIKit/UIKit.h>
#import "SettingViewController.h"

@interface RootViewController : UIViewController<SetFontSizeProtocol>


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


- (IBAction)goToSetting:(id)sender;


@end


//
//  ViewController.m
//  Protocol_demo
//
//  Created by qianfeng on 13-5-23.
//  Copyright (c) 2013骞?qianfeng. All rights reserved.
//


#import "RootViewController.h"


@interface RootViewController ()


@end


@implementation RootViewController


- (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.
}


- (IBAction)goToSetting:(id)sender
{
    
    SettingViewController *settingViewController = [[SettingViewController alloc]init];
    
    settingViewController.delegate = self;
    /**
    协议三部曲
     1、遵循协议
     2、实现方法
     3、设置代理,(设置代理是为了能在发出通知的对象中,通过delegate调用实现了的协议方法)

   */
    
    settingViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:settingViewController animated:YES completion:NULL];
    [settingViewController release];
    
}


- (void)changeFontSize:(float)size
{
    
    _label.font = [UIFont systemFontOfSize:size];


}


@end


//
//  SettingViewController.h
//  Protocol_demo
//
//  Created by qianfeng on 13-5-23.
//  Copyright (c) 2013骞?qianfeng. All rights reserved.
//


#import <UIKit/UIKit.h>


@protocol SetFontSizeProtocol <NSObject>


- (void)changeFontSize:(float)size;


@end


@interface SettingViewController : UIViewController


@property (retain, nonatomic) IBOutlet UILabel *demoLabe;
@property (assign, nonatomic) id<SetFontSizeProtocol> delegate;
- (IBAction)setFontSize:(UISlider *)sender;


@end


//
//  SettingViewController.m
//  Protocol_demo
//
//  Created by qianfeng on 13-5-23.
//  Copyright (c) 2013骞?qianfeng. All rights reserved.
//


#import "SettingViewController.h"


@interface SettingViewController ()


@end


@implementation SettingViewController


- (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 from its nib.
    
    UINavigationBar *navigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    [navigationBar setBarStyle:UIBarStyleBlackTranslucent];
    
    UINavigationItem *navigationItem = [[UINavigationItem alloc]initWithTitle:@"Setting"];
    UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(goBack)];
    navigationItem.leftBarButtonItem = backBarButton;
    [backBarButton release];
    
    [navigationBar pushNavigationItem:navigationItem animated:NO];
    [navigationItem release];
    [self.view addSubview:navigationBar];
    
}


- (void)goBack
{
    
    [self dismissViewControllerAnimated:YES completion:NULL];


}


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


- (IBAction)setFontSize:(UISlider *)sender {
    
    float size = sender.value;
    _demoLabe.font = [UIFont systemFontOfSize:size];
    
    [self.delegate changeFontSize:size];
    
}
- (void)dealloc {
    [_demoLabe release];
    [super dealloc];
}
@end


原创粉丝点击