温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

[IOS]Setting Bundle + StoryBoard

发布时间:2020-08-09 18:36:58 来源:网络 阅读:322 作者:蓬莱仙羽 栏目:移动开发

用storyboard添加一个导航栏,其中首页有一个switch,与setting联动,还有一个button,使用modal连接另一个viewControl,其上也有一个按钮,按下销毁本viewControl,回到前一页。

实现步骤:

1.创建一个SingleView的项目,勾选上storyboard。

2.向storyboard中添加一个NavigationController,两个ViewController,然后在NavigationController中右击指向第一个ViewController,然后设置为rootViewController,并且将箭头指向NavigationController。

3.将两个ViewController的Class分别设置为DXWViewController和DXWViewController1(两个自己创建的类,继承自ViewController)

4.创建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"]?@"开":@"关";     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]; } //实现协议的方法 - (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"]?@"开":@"关"; } - (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方法是通过代码的方法实现跳转到下一个view,如果是通过storyboard实现连线的方法然后跳过下一个view是这样实现:

@“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问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI