Modal View Controller Example – Part 2[转]

n the first part of this tutorial, we set up a pair of simple views in Interface Builder that we switched between modally. In this tutorial, we’ll make them somewhat useful and pass data between them using delegates.

The concept of protocols and delegates is an important and somewhat complex one, but I like to think of it in these simplified terms:

Basically, the object that implements our protocol agrees to implement the methods of that protocol. In the case of this tutorial, we’ll be connecting the modal view with our main view using a delegate.

Firstly, we’ll create the interface elements for our project. Open the ModalViewExampleViewController XIB file and create a button and a label as shown.

Next, add those interface elements to ModalViewExampleViewController.h. We’re also adding the necessary IBAction also:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19


//  ModalViewExampleViewController.h

 

@interface ModalViewExampleViewController : UIViewController {

    UIButton *showDefaultButton, *showFlipButton, *showDissolveButton, *showCurlButton;

    UIButton *showWithDelegateButton;

    UILabel *myMessage;

}

 

@property (nonatomic, retain) IBOutlet UIButton *showDefaultButton, *showFlipButton, *showDissolveButton, *showCurlButton;

@property (nonatomic, retain) IBOutlet UIButton *showWithDelegateButton;

@property (nonatomic, retain) IBOutlet UILabel *myMessage;

 

- (IBAction)showDefault:(id)sender;

- (IBAction)showFlip:(id)sender;

- (IBAction)showDissolve:(id)sender;

- (IBAction)showCurl:(id)sender;

- (IBAction)showWithDelegate:(id)sender;

 

@end

Be sure to include the necessary additions to ModalViewExampleViewController.m:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24


//  ModalViewExampleViewController.m

#import "ModalViewExampleViewController.h"

#import "SampleViewController.h"

 

@implementation ModalViewExampleViewController

 

@synthesize showDefaultButton, showFlipButton, showDissolveButton, showCurlButton;

@synthesize showWithDelegateButton, myMessage;

...

- (IBAction)showWithDelegate:(id)sender {

 

}

...

- (void)dealloc {

    [showDefaultButton release];

    [showFlipButton release];

    [showDissolveButton release];

    [showCurlButton release];

    [showWithDelegateButton release];

    [myMessage release];

    [super dealloc];

}

 

@end

Jump to Interface Builder and be sure to link the new elements with the properties we defined. Refer to Part 1 of this tutorial for a guide on how to do that.

The next step is key. We will create a basic protocol and then assign a delegate. Open up ModalViewExampleViewController.h and add this:


1

2

3

4

5

6


// ModalViewExampleViewController.h

@protocol ModalViewDelegate

 

- (void)didReceiveMessage:(NSString *)message;

 

@end

We then tell ModalViewExampleViewController to implement this protocol:

// ModalViewExampleViewController.h

@interface ModalViewExampleViewController : UIViewController <ModalViewDelegate>

We also need to add the protocol’s method to the main implementation:


1

2

3

4


// ModalViewExampleViewController.m

- (void)didReceiveMessage:(NSString *)message {

 

}

Once we have these in place, the next step is to set up a reference between the two views. What we will do is define a delegate inside of SampleView so that we can send messages to it.
Include the protocol in ModalViewExampleViewController.h and add the reference:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15


// SampleViewController.h

@protocol ModalViewDelegate;

 

@interface SampleViewController : UIViewController {

    id<ModalViewDelegate> delegate;

 

    UIButton *dismissViewButton;

}

 

@property (nonatomic, assign) id<ModalViewDelegate> delegate;

@property (nonatomic, retain) IBOutlet UIButton *dismissViewButton;

 

- (IBAction)dismissView:(id)sender;

 

@end

Be sure to synthesize the delegate in SampleViewController.m, and include the necessary header files.


1

2

3

4

5

6

7

8


#import "SampleViewController.h"

#import "ModalViewExampleViewController.h"

 

@implementation SampleViewController

 

@synthesize dismissViewButton;

@synthesize delegate;

...

So far we have defined a protocol inside of our parent view, and defined a delegate in our modal view. The next step is to link them together and make them useful.
Firstly, we’ll write the functions that will handle the messages. Replace the original definition of didReceiveMessage with this:


1

2

3


- (void)didReceiveMessage:(NSString *)message {

[myMessage setText:message];

}

And also add the following code to showWithDelegate:


1

2

3


 SampleViewController *sampleView = [[[SampleViewController alloc] init] autorelease];

 sampleView.delegate = self;

 [self presentModalViewController:sampleView animated:YES];

What we’ve done is create a SampleView object and assigned its delegate to be the parent view. That is, the parent view will be handling messages sent by SampleView.
Open up SampleViewController.m and add the code to send the message.


1

2

3

4

5

6


// SampleViewController.m

- (IBAction)dismissView:(id)sender {

    [delegate didReceiveMessage:@"Hello World"];

 

    [self dismissModalViewControllerAnimated:YES];

}

Compile the app and run it. You should be able to see the text “Hello World” passed from one view to another once you dismiss your modal view with delegate. You can extend this any way you like with additional controls on the modal view, such as sliders or text input.

Download the source code for this project here.

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2011/11/22/2259441.html

时间: 2024-10-30 09:04:32

Modal View Controller Example – Part 2[转]的相关文章

ios-报错:nil modal view controller

问题描述 报错:nil modal view controller 得到了如下报错:**Application tried to present a nil modal view controller on target**.我在运行的程序是,首先判断条件是否满足,启动后修改初始化视图控制器. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launch

Modal View Controller Example[转]

In your iPhone app, you'll probably be spending most of the time pushing new view controllers to the stack in order to show screen flow. Sometimes, though, you just want to popup a screen for quick display or input. Here's a quick demo/tutorial on th

Modal View Controller的不同呈现方式

  ModalViewController可以有不同的呈现方式(modalPresentationStyle),在ipad下要提供多方向支持时,就要注意可能要改变ModalViewController的呈现方式,列举如下: UIModalPresentationFullScreen:全屏模式,即弹出窗口占满整个屏幕,在portrait模式和landscape模式下都一样, UIModalPresentationFormSheet:会将窗口缩小,使之居于屏幕中间,在portrait和landsca

用Swift完成不同View Controller之间的切换

之前用objective-c开发时,页面之间的切换很容易.其实用swift没有很大的变化,如果你是用storyboard完成的界面,基本上是同样的方式,只不过在代码部分写成swift风格的就行了. 今天在实验开发一个简单的小程序时,却遇到了一些bug,后来还是求助stackoverflow上的大神解决了问题,在此做下记录. 我的程序结构是这样的,在一个页面A中有个按钮,然后点击按钮以后,切换到另一个页面B.A和B都在同一个storyboard中. 这里先说下通用的方法: 手动用代码建好的view

Applications are expected to have a root view controller at the end of application launch

问题:Applications are expected to have a root view controller at the end of application launch 环境:XCode4.2 场景:这种问题多发生在XCode4.2 移植低版本项目时出现. 原因:在iOS5下,应用加载时,需要一个root view controller,在iOS5以下的版本会有MainWindow作为启动文件,iOS5以上没有了. 解决方案:手动创建一个root view controller,

Swift:超炫的View Controller切换动画

匿名社交应用Secret的开发者开发了一款叫做Ping的应用,用户可以他们感兴趣的话题的推送. Ping有一个很炫的东西,就是主界面和之间切换的动画做的非常的好.每次看到一个非常炫的动画,都不由得会想:"这个东西我要不要自己实现以下".哈哈~~~ 这个教程里,你会学到如何用Swift实现这样的很酷的动画.你会学到如何使用shape layer,遮罩和使用UIViewControllerAnimnatedTransitioning协议和UIPercentDrivenInteractive

ios-释放root view controller

问题描述 释放root view controller 在下面语法中有声明: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 声明是: root_view_controller = [[Root_View_Controller alloc] initWithNibName:@"Base_View" bundle : n

view controller内部全部视图旋转

iOS屏幕旋转控制在View Controller里面,包含三种controller.    其一:UIViewController及其子类.    其二:UINavigationController及其子类.    其三:UITabBarController及其子类.    每一种controller及其子类都可以写屏幕旋转控制代码.但是记住一个原则,谁加载谁获得屏幕控制的权限,被加载的controller如果要添加自适应代码,可以在- (void)willRotateToInterfaceO

Methods throughout the lifespan of a view controller

Method                                DescriptionloadView                              Creates or returns a view for the view controller.viewDidLoad                       View has finished loading.viewWillAppear:                    View is about to a