#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "FirstViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc{
[_window release];
[super dealloc];
}
int sum(int x, int y){
return x + y;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
NSLog(@"%d", sum(2, 3));
int (*method)(int x, int y) = sum;
NSLog(@"%d", method(3, 5));
int (^block1)(int x, int y);
block1 = ^(int a, int b){
return a + b;
};
block1 = ^(int a, int b){
return a * b;
};
NSLog(@"%d", block1(10, 10));
NSInteger (^block2)(NSString *str);
block2 = ^(NSString *str){
return [str integerValue];
};
NSLog(@"%ld", block2(@"123"));
FirstViewController *firstVC = [[FirstViewController alloc] init];
firstVC.title = @"第一个页面";
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:firstVC];
self.window.rootViewController = navC;
[navC release];
[firstVC release];
return YES;
}
@end
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@end
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@property (nonatomic, retain) UITextField *textField;
@end
@implementation FirstViewController
- (void)dealloc{
[_textField release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
_textField = [[UITextField alloc] initWithFrame:(CGRectMake(50, 100, 200, 50))];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.backgroundColor = [UIColor grayColor];
[self.view addSubview:_textField];
UIBarButtonItem *nextBarButton = [[UIBarButtonItem alloc] initWithTitle:@"next" style:(UIBarButtonItemStylePlain) target:self action:@selector(nextAction:)];
self.navigationItem.rightBarButtonItem = nextBarButton;
[nextBarButton release];
}
- (void)nextAction:(UIBarButtonItem *)barButton{
SecondViewController *secondVC = [[SecondViewController alloc] init];
__block typeof (self) blockSelf = self;
__weak typeof (self) weakSelf = self;
secondVC.secondBlock = ^(NSString *text){
self.textField.text = text;
};
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import <UIKit/UIKit.h>
typedef void(^MyBlock) (NSString *string);
@interface SecondViewController : UIViewController
@property (nonatomic, retain) UITextField *textField;
@property (nonatomic, copy) MyBlock secondBlock;
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)dealloc{
[_textField release];
Block_release(_secondBlock);
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
_textField = [[UITextField alloc] initWithFrame:(CGRectMake(50, 100, 200, 50))];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.backgroundColor = [UIColor grayColor];
[self.view addSubview:_textField];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction:)];
self.navigationItem.rightBarButtonItem = backBarButton;
[backBarButton release];
}
- (void)backAction:(UIBarButtonItem *)barButtonItem{
_secondBlock(_textField.text);
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end