模態視圖(IOS開發),模態視圖ios開發

來源:互聯網
上載者:User

模態視圖(IOS開發),模態視圖ios開發

模態:模態視圖從螢幕下方滑出來,完成的時候需要關閉這個模態視圖,如果不關閉,就不能做別的事情,必須有響應處理的含義。

主視圖控制器---》模態視圖控制器。主視圖控制器與模態視圖控制器之間為父子關係。

UIViewController類中,主要有以下兩個方法:

presentViewController:animated:completion  呈現模態視圖

dismissViewControllerAnimated:completion 關閉模態視圖




代碼:

ViewController.h

#import <UIKit/UIKit.h>@interface ViewController : UIViewController- (IBAction)regonclick:(id)sender;@end

ViewController.m

#import "ViewController.h"#import "RegisterViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // 如果要傳遞參數的時候,應該用委託設計模式或者廣播通知機制    // 這裡為廣播通知機制    // 註冊一個自訂通知RegisterCompletionNotification,通知到來時候發出registerCompletion:訊息,其參數notification中可以包含回傳的參數,統一放在NSDictionary字典中    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(registerCompletion:) name:@"RegisterCompletionNotification" object:nil];    }- (void)registerCompletion:(NSNotification *)notification{    // 擷取參數userInfo,是一個字典    NSDictionary *theData = [notification userInfo];    // 擷取字典的username索引值    NSString *username = [theData objectForKey:@"username"];        NSLog(@"username = %@", username);}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (IBAction)regonclick:(id)sender {    // 使用registerViewController的ID擷取視圖控制器對象    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];    UIViewController *registerViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"registerViewController"];        // 設定模態視圖呈現和關閉時候的動畫效果    // 垂直方向由底向上退出    registerViewController.modalPresentationStyle = UIModalTransitionStyleCoverVertical;        // 呈現完成時調用completion代碼塊    // 代碼塊問題回頭再說    [self presentViewController:registerViewController animated:YES completion:^{        NSLog(@"Present Modal View");    }];}@end

ResgisterViewController.h

#import <UIKit/UIKit.h>@interface RegisterViewController : UIViewController- (IBAction)done:(id)sender;@property (weak, nonatomic) IBOutlet UITextField *txtUsername;@end

ResgisterViewController.m

#import "RegisterViewController.h"@interface RegisterViewController ()@end@implementation RegisterViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // 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)done:(id)sender {    // 關閉模態視圖,然後繼續調用代碼塊    [self dismissViewControllerAnimated:YES completion:^{        // 代碼塊        NSLog(@"Modal View done");                // 建立一個以username為索引的字典對象        NSDictionary *dataDict = [NSDictionary dictionaryWithObject:self.txtUsername.text forKey:@"username"];        [[NSNotificationCenter defaultCenter] postNotificationName:@"RegisterCompletionNotification" object:nil         userInfo:dataDict];     }];        }@end



iOS開發視頻教程 UI:37 模態視圖、控制方向mov種子有?感謝哈

iOS開發視頻教程 UI:3.7 模態視圖、控制方向.mov種子:
thunder://QUFodHRwOi8vYWlrYW5keS5vcmcvaU9T5byA5Y+R6KeG6aKR5pWZ56iLIFVJ77yaMy43IOaooeaAgeinhuWbvuOAgeaOp+WItuaWueWQkS5tb3Y/ZmlkPXVINEdlZ25RckNacFJkKnZXamVodEhTUDE0Y0FBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUEmbWlkPTY2NiZ0aHJlc2hvbGQ9MTUwJnRpZD1FNTFERDdCQzAxQTkyODJCQTc3RUMxQzNCRUIzQ0JGNyZzcmNpZD0xMjAmdmVybm89MVpa
鄙視拿種子就走滴人!!!
 
ios開發:怎實現點擊一個按鈕,跳轉到一個新的介面,急阿,~~~

1.可以使用導航控制器棧。將當前視圖控制器作為rootViewController.需要在建立當前控制器的代碼中這樣來建立
UIViewController *vc1=[[UIViewControlelr alloc] init];
UINavigationController *navController =[[UINavigationController alloc] initWithRootViewController:vc1];
[vc1 release];
[window addSubView:navController.view];
[navController release];

只有噹噹前控制器在導航控制器棧中才可以使用pushViewController來導航其它視圖
導航到新的視圖控制器:
UIViewController *vc2=[[ViewController alloc] init];
[self.navigationController pushViewController:vc2 animated:YES];
[vc2 release];
2.模態視圖
UIViewController *vc2=[[ViewController alloc] init];

[self presentModalViewController:controller animated:YES];
[vc2 release];

3.使用新的視圖覆蓋當前視圖
如果使用這種方式,建議建立一個可維護控制器之間互動的控制器swithController,在這個控制器中來實現不同控制器之間的視圖切換
@inertface SwitchViewController:UIViewController
@property(retain) UIViewController *vc1
@property (retain) UIViewController *vc2;

-(void)showVC1;
-(void)showVC2;
@end

@implementation SwitchViewController
@synthesize vc1,vc2;

-(void)showVC1 {
if (vc2) {
[vc2.view removeFromSuperView];
}

[self.view addSubView:vc1.view];
}
@end
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.