IOS UI-Block
来源:互联网 发布:mac 口红brave 编辑:程序博客网 时间:2024/06/17 02:14
在Xib下建立工程,创建两个页面,有两个button,点击第一个页面的button push 出第二个页面,并且利用block传值,把第一个页面的button值传给第二页 再把第二个页面button名字传给第一页
程序如下:
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate :UIResponder <UIApplicationDelegate>
@property (strong,nonatomic) UIWindow *window;
@end
APPDelegate.m
#import "AppDelegate.h"
#import "MainViewController.h"
@interfaceAppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_windowrelease];
[superdealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
[_windowrelease];
MainViewController *mainVC = [[MainViewControlleralloc]init];
UINavigationController *naviVC = [[UINavigationControlleralloc]initWithRootViewController:mainVC];
self.window.rootViewController = naviVC;
[mainVCrelease];
[naviVCrelease];
returnYES;
}
MainViewController.h
#import <UIKit/UIKit.h>
@interface MainViewController :UIViewController
@end
MainViewController.m#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()
- (IBAction)buttonAction:(UIButton *)sender;
@end
@implementation MainViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)buttonAction:(UIButton *)sender {
NSLog(@"测试");
//Block 的基本形式
//无返回值 无参数
void (^block1) (void) = ^(void){
NSLog(@"adas");
};
//给block变量赋值
block1();//block在方法里面回车
//无返回值有参数
void(^block2)(NSInteger num) = ^(NSInteger num){
NSLog(@"无返回值,有参数");
NSLog(@"%ld",num);
};
block2(5);
//参数是字符串的block ,有一个字符串参数的block变量
void (^block3) (NSString *str) = ^(NSString *str){
// NSLog(@"芊芊");
NSLog(@"%@",str);
//利用字符串参数改变button的标题
[sender setTitle:strforState:UIControlStateNormal];
};
// //调用block改变button标题
// block3(@"芊芊");
// 让第二个页面接收block
SecondViewController *secVC = [[SecondViewControlleralloc]init];
//从前往后传值2
secVC.name = sender.currentTitle;
//2.3 在第一个页面,将定义好的block变量给第二页
secVC.block = block3;
[self.navigationControllerpushViewController:secVC animated:YES];
[secVC release];
}
@end
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
//从前往后传值1
@property (nonatomic,retain)NSString *name;
//给第二个页面写一个block
//2.1
//block的属性需要copy修饰,将block变量从栈区拷贝到堆区
@property (nonatomic,copy)void(^block) (NSString *str);
@end
SecondViewContrller.m#import "SecondViewController.h"
//函数
//返回值 方法名 (类名)
//调用 方法名();
//把两个函数的方法名擦掉 是类型
//地址 把名字去掉加* 指针(* p)
//block 就是把*换成^
@interface SecondViewController ()
@property (retain,nonatomic) IBOutletUITextField *textField;
- (IBAction)buttonAction:(UIButton *)sender;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view from its nib.
//从前往后传值3 接收
self.textField.text =self.name;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)buttonAction:(UIButton *)sender {
[self.navigationControllerpopToRootViewControllerAnimated:YES];
//2.2
//调用block,将修改后的textField中的字符串用于改变第一个页面的button标题
self.block(_textField.text);
}
- (void)dealloc {
[_textField release];
[super dealloc];
}
@end
SecondViewController.xibMainViewController.xib
最后运行效果:
- IOS UI-Block
- IOS笔记UI--Block传值
- iOS UI 12 block传值
- block-ui
- UI - Block
- ios block
- IOS BLOCK
- ios block
- ios block
- ios block
- IOS BLOCK
- ios block
- iOS Block
- ios block
- IOS block
- iOS Block
- IOS block
- iOS BLOCK
- ubuntu下pppoe服务器的搭建以及设置串口为默认控制台
- Python调用C++程序备忘笔记
- hash_map和map的区别
- 天猫首页迷思之-jquery实现整个div的懒加载(2)-插件面向对象化-闭包和原型的实例
- unity 3d学习第二天
- IOS UI-Block
- MyBatis Eclipse插件安装及generatorConfig.xml配置详解
- Hibernate中get()和load()区别
- EXTJS开发过程中,遇到的问题和解决办法 15条经验
- mipi 的LCD调试经验
- 将任意类型数组变为集合
- 无线路由器开启或关闭DHCP
- %I64d %I64u
- 数据结构中排序算法- 图书馆排序(6)