您的位置:首页 > 产品设计 > UI/UE

UI - Block

2015-10-18 15:16 288 查看
<AppDelegate.m>

#import "AppDelegate.h"
#import "FirstViewController.h"
@implementation AppDelegate

- (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];
[self.window makeKeyAndVisible];

FirstViewController *firstVC = [[FirstViewController alloc]init];
UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:firstVC];
self.window.rootViewController = naVC;
[firstVC release];
[naVC release];

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


<FirstViewController.h>

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end


<FirstViewController.m>

#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()//<SecondViewControllerDelegate>
@property (nonatomic,retain)UITextField *tf;
@end

@implementation FirstViewController

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

//textfield
self.tf = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 220, 40)];
_tf.backgroundColor = [UIColor cyanColor];
_tf.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_tf];
[_tf release];

//设置rightItem  push 下一个界面
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"push" style:UIBarButtonItemStyleDone target:self action:@selector(push:)];
[self.navigationItem.rightBarButtonItem release];

}

//-(void)sendString:(NSString *)string
//{
//    _tf.text = string;
//}

-(void)push:(UIBarButtonItem *)rightItem
{
SecondViewController *secondVC = [[SecondViewController alloc]init];
[self.navigationController pushViewController:secondVC animated:YES];

secondVC.string = _tf.text;
//    secondVC.delegate = self;

//对于 block 语法块内部  如果引用了外部对象 就会对外部对象引用计数 +1, 持有一份所有权, 如果想解决这个问题就对对象使用 __block 进行修饰

//    __block SecondViewController *aa = secondVC;
__block FirstViewController *bb = self;
//对 secondVC passvalueblock 进行赋值 设置方法的执行过程
secondVC.passValueBlock = ^ void (NSString *string) {
//将传入的字符串赋值给 textField
bb.tf.text = string;
//        aa.string = @"aa";
};
[secondVC release];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}

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

@end


<SecondViewController.h>

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

@protocol SecondViewControllerDelegate <NSObject>

-(void)sendString:(NSString *)string;

@end

//重新定义
typedef void (^PassValueBlock)(NSString *);

@interface SecondViewController : UIViewController
@property (nonatomic,copy)NSString *string;
@property (nonatomic,assign)id<SecondViewControllerDelegate>delegate;
//声明了一个 passvalueblock 的属性
@property (nonatomic,copy)PassValueBlock passValueBlock;  //只能用 copy. 当用 assign 的话,它在栈区存放,会自动释放,就会出现野指针问题,不可以传值. 又因为 block 语法块中的内容并不是对象,所以不可以用 retain,  所以用 copy
@end


<SecondViewController.m>

#import "SecondViewController.h"

@interface SecondViewController ()
@property (nonatomic,retain)UITextField *tf;
@end

@implementation SecondViewController

- (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.
self.view.backgroundColor = [UIColor whiteColor];

self.tf = [[UITextField alloc ]initWithFrame:CGRectMake(50, 100, 220, 40)];
_tf.backgroundColor = [UIColor orangeColor];
_tf.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_tf];
[_tf release];

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"pop" style:UIBarButtonItemStyleDone target:self action:@selector(pop:)];
[self.navigationItem.leftBarButtonItem release];

//属性赋值
_tf.text = _string;

}
-(void)pop:(UIBarButtonItem *)leftItem
{
[self.navigationController popToRootViewControllerAnimated:YES];
//    [_delegate sendString:_tf.text];
//调用 block
self.passValueBlock(_tf.text);

}
-(void)dealloc
{
Block_release(self.passValueBlock);
[super dealloc];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: