通知的用法

        这个工程创建一个根视图控制器,加载在widow上。并且在其视图上添加一个label标签和button按钮,label标签text设置为helloworld,当单击button时,弹一个模态视图,模态视图中包含一个返回按钮,返回根视图时,将label标签的值设定为textField的值,用通知方法实现。


#pragma ----AppDelegate.h-----

#import <UIKit/UIKit.h>

@class RootViewController;


@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;

@property (retain,nonatomic) RootViewController *myRootvc;


@end




#pragma ----AppDelegate.m-----

#import "AppDelegate.h"

#import "RootViewController.h"


@implementation AppDelegate


- (void)dealloc

{

    [_window release];

    [_myRootvc 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.window.backgroundColor = [UIColor whiteColor];

    RootViewController *tempMyRootVC = [[RootViewController alloc]init];

    self.myRootvc = tempMyRootVC;

    [tempMyRootVC release];

    self.window.rootViewController = self.myRootvc;

    

    [self.window makeKeyAndVisible];

    return YES;

}


@end



#pragma ----RootViewController.h----

#import <UIKit/UIKit.h>

@class ModalViewController;

@interface RootViewController : UIViewController


@end



#pragma ----RootViewController.m----

#import "RootViewController.h"

#import "ModalViewController.h"


@interface RootViewController ()


@end


@implementation RootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor purpleColor];

    

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    myButton.frame = CGRectMake(90, 200, 150, 40);

    [myButton setTitle:@"Present" forState:UIControlStateNormal];

    [myButton addTarget:self action:@selector(presentModalVC) forControlEvents:UIControlEventTouchUpInside];

    myButton.backgroundColor = [UIColor redColor];

    [self.view addSubview:myButton];

    

    UILabel *tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 150, 140, 40)];

    tempLabel.text = @"Hello world";

    tempLabel.tag = 102;

    tempLabel.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:tempLabel];

    [tempLabel release];

    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLabelText:) name:@"ChangeLabelTextNotification" object:nil];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


-(void)presentModalVC

{

    ModalViewController *myModalVC = [[ModalViewController alloc]init];

    

    

    myModalVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;

    [self presentViewController:myModalVC animated:YES completion:^{

        NSLog(@"callback") ;

    }];

}


-(void)changeLabelText:(NSNotification*)notification

{

    id text = notification.object;

    UILabel *label = (UILabel*)[self.view viewWithTag:102];

    label.text = text;

}


-(void)dealloc

{

    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"ChangeLabelTextNotification" object:nil];

    [super dealloc];

}


@end


#pragma ----ModalViewController.h---

#import <UIKit/UIKit.h>


@protocol ModalViewControllerDelegate <NSObject>


@optional

-(void)changeLabelText:(NSString*)text;


@end


@interface ModalViewController : UIViewController


{

    @private

    UITextField *_myTextField;

}



@end


#pragma ----ModalViewController.m---

#import "ModalViewController.h"


@interface ModalViewController ()


@end


@implementation ModalViewController


-(void)dealloc

{

    [_myTextField release];

    [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];

    self.view.backgroundColor = [UIColor blueColor];

    

    _myTextField = [[UITextField alloc]initWithFrame:CGRectMake(70, 200, 180, 40)];

    [self.view addSubview:_myTextField];

    _myTextField.borderStyle = UITextBorderStyleRoundedRect;

    

    

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    myButton.frame = CGRectMake(90, 150, 140, 40);

    [myButton setTitle:@"dismiss" forState:UIControlStateNormal];

    [myButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];

    myButton.backgroundColor = [UIColor redColor];

    [self.view addSubview:myButton];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


-(void)dismiss 

{

    [[NSNotificationCenter defaultCenter]postNotificationName:@"ChangeLabelTextNotification" object:_myTextField.text];


    

    [self dismissViewControllerAnimated:YES completion:^{

        NSLog(@"dismiss");

    }];

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值