溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

[IOS]Setting Bundle + StoryBoard

發(fā)布時間:2020-08-09 18:36:58 來源:網(wǎng)絡(luò) 閱讀:333 作者:蓬萊仙羽 欄目:移動開發(fā)

用storyboard添加一個導(dǎo)航欄,其中首頁有一個switch,與setting聯(lián)動,還有一個button,使用modal連接另一個viewControl,其上也有一個按鈕,按下銷毀本viewControl,回到前一頁。

實現(xiàn)步驟:

1.創(chuàng)建一個SingleView的項目,勾選上storyboard。

2.向storyboard中添加一個NavigationController,兩個ViewController,然后在NavigationController中右擊指向第一個ViewController,然后設(shè)置為rootViewController,并且將箭頭指向NavigationController。

3.將兩個ViewController的Class分別設(shè)置為DXWViewController和DXWViewController1(兩個自己創(chuàng)建的類,繼承自ViewController)

4.創(chuàng)建setting文件,并將root.plist保存一個鍵值對,key改成switch

5.修改DXWViewController(主視圖)

[IOS]Setting Bundle + StoryBoard

DXWViewController.h:

#import <UIKit/UIKit.h> #import "DXWViewController1.h" @interface DXWViewController : UIViewController<DXWFlipsideViewControllerDelegate> - (IBAction)change:(id)sender; @property (retain, nonatomic) IBOutlet UILabel *label; @property (retain, nonatomic) IBOutlet UISwitch *switchButton;  - (IBAction)showInfo:(id)sender;  @end

DXWViewController.m:

#import "DXWViewController.h"  @interface DXWViewController ()  @end  @implementation DXWViewController  -(void)viewWillAppear:(BOOL)animated {     [self changeData]; }  -(void)changeData {     NSUserDefaults *usr = [NSUserDefaults standardUserDefaults];     NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@YES,@"switch", nil];          [usr registerDefaults:dic];     ((UILabel *)self.label).text = [usr boolForKey:@"switch"]?@"開":@"關(guān)";     self.switchButton.on = [usr boolForKey:@"switch"];     //都要寫入一下     [usr synchronize]; }  - (void)viewDidLoad {     [super viewDidLoad]; 	UIApplication *app = [UIApplication sharedApplication];     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeData) name:UIApplicationWillEnterForegroundNotification object:app]; }   - (void)dealloc {     [self.switchButton release];     [_label release];     [super dealloc]; } //實現(xiàn)協(xié)議的方法 - (void)flipsideViewControllerDidFinish:(DXWViewController1 *)controller {     [self dismissViewControllerAnimated:YES completion:nil]; } - (IBAction)change:(id)sender {     UISwitch *switchButton = sender;     NSUserDefaults *user = [NSUserDefaults standardUserDefaults];     [user setBool:switchButton.on forKey:@"switch"];     [user synchronize];     ((UILabel *)self.label).text = [user boolForKey:@"switch"]?@"開":@"關(guān)"; } - (IBAction)showInfo:(id)sender {     UIStoryboard *strBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];     DXWViewController1 *controller = [strBoard instantiateViewControllerWithIdentifier:@"DXWViewController1"];     controller.delegate = self;     controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;     [self presentViewController:controller animated:YES completion:nil]; } @end


上圖中showInfo方法是通過代碼的方法實現(xiàn)跳轉(zhuǎn)到下一個view,如果是通過storyboard實現(xiàn)連線的方法然后跳過下一個view是這樣實現(xiàn):

@“Add”是連線的ID
//連線的方法 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {     if ([segue.identifier isEqualToString:@"Add"]) {         DXWViewController1 *controlller = segue.destinationViewController;         controlller.delegate = self;     } }


6.修改DXWViewController1(子視圖)

[IOS]Setting Bundle + StoryBoard

DXWViewController.h:

#import <UIKit/UIKit.h> @class DXWViewController1;  @protocol DXWFlipsideViewControllerDelegate - (void)flipsideViewControllerDidFinish:(DXWViewController1 *)controller; @end @interface DXWViewController1 : UIViewController @property (assign, nonatomic) id <DXWFlipsideViewControllerDelegate> delegate; - (IBAction)done:(id)sender; @end

DXWViewController.m:

#import "DXWViewController1.h"  @interface DXWViewController1 ()  @end  @implementation DXWViewController1  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self) {         // Custom initialization     }     return self; }  - (void)viewDidLoad {     [super viewDidLoad]; 	// Do any additional setup after loading the view. }  - (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning];     // Dispose of any resources that can be recreated. }  - (IBAction)done:(id)sender {     [self.delegate flipsideViewControllerDidFinish:self]; } @end




向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI