1
2
3
4
5
|
// 这里按钮按下的时候退出 ModalViewController
-(
void
)dismiss:(
id
)inSender {
// 如果是被 presentModalViewController 以外的实例调用,parentViewController 将是nil,下面的调用无效
[
self
.parentViewController dismissModalViewControllerAnimated:
YES
];
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
|
- (
void
)applicationDidFinishLaunching:(UIApplication *)application {
controller = [[CustomViewController alloc] init];
[window addSubview:controller.view];
[window makeKeyAndVisible];
// 生成 ModalViewController
CustomViewController* controllerB = [[CustomViewController alloc] init];
// 设置 view 的背景为红色
controllerB.view.backgroundColor = [UIColor redColor];
// 显示 ModalViewController view
[controller presentModalViewController:controllerB animated:
YES
];
// presentModalViewController 已经被 controller 管理,这里可以释放该实例了
[controllerB release];
}
|
1
|
controllerB.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
- (
void
)viewDidLoad {
[
super
viewDidLoad];
self
.view.backgroundColor = [UIColor blueColor];
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100,100,100,100);
button.tag = 1;
[button setTitle:
@"绿色"
forState:UIControlStateNormal];
// 按钮事件对应函数
[button addTarget:
self
action:
@selector
(dismiss:)
forControlEvents:UIControlEventTouchUpInside];
[
self
.view addSubview:button];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100,200,100,100);
button.tag = 2;
[button setTitle:
@"灰色"
forState:UIControlStateNormal];
// 按钮事件对应函数
[button addTarget:
self
action:
@selector
(dismiss:)
forControlEvents:UIControlEventTouchUpInside];
[
self
.view addSubview:button];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100,300,100,100);
button.tag = 0;
[button setTitle:
@"取消"
forState:UIControlStateNormal];
// 按钮事件对应函数
[button addTarget:
self
action:
@selector
(dismiss:)
forControlEvents:UIControlEventTouchUpInside];
[
self
.view addSubview:button];
}
|
1
|
-(
void
)selectColor:(UIColor*)inColor;
|
1
2
3
|
@interface
CustomViewController : UIViewController {
id
colorSelectDelegate;
}
|
1
2
3
|
-(
void
)setColorSelectDelegate:(
id
)inDelegate {
colorSelectDelegate = inDelegate;
}
|
1
2
3
4
5
6
7
8
9
|
-(
void
)dismiss:(
id
)inSender {
UIView* view = (UIView*)inSender;
UIColor* requestColor =
nil
;
if
(view.tag == 1)
requestColor = [UIColor greenColor];
if
(view.tag == 2)
requestColor = [UIColor grayColor];
[colorSelectDelegate selectColor:requestColor];
}
|
1
2
3
4
5
|
-(
void
)selectColor:(UIColor*)inColor {
if
(inColor !=
nil
)
self
.view.backgroundColor = inColor;
[
self
dismissModalViewControllerAnimated:
YES
];
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
- (
void
)applicationDidFinishLaunching:(UIApplication *)application {
controller = [[CustomViewController alloc] init];
[window addSubview:controller.view];
[window makeKeyAndVisible];
// 创建 ModalViewController view 的 Controller
CustomViewController* controllerB = [[CustomViewController alloc] init];
// 设置背景色为红色
controllerB.view.backgroundColor = [UIColor redColor];
// 设置委托实例
[controllerB setColorSelectDelegate:controller];
// 显示 ModalViewController view
[controller presentModalViewController:controllerB animated:
YES
];
[controllerB release];
}
|
编译一下,程序启动后显示红色背景的 ModalViewController view,点击绿色按钮后,原先的view的背景变为绿色,点击灰色,显示灰色的背景,而点击取消,那么将显示原先蓝色的背景。
这样的形式,就是将按钮的动作委托给原先view的 Controller 来处理了。根据送来的 UIColor 来设置不同的背景色。
原文:http://bbs.gdchina.net/Game-Dev-396-1-1.html