iOS UIStoryBoard使用笔记(一)

本文介绍了iOS开发中UIStoryBoard的使用条件、特性,详细讲解了如何进行传值和逻辑处理,以及UIStoryboardSegue的相关属性和应用实例。通过阅读,开发者可以更好地理解和运用UIStoryBoard进行页面间的跳转和数据传递。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目进入测试阶段,偷得半日闲整理这篇博客,码字不容易,喜欢转载请大家标明出处:http://blog.youkuaiyun.com/wangtie_123/article/details/36179191

 UIStoryBoard 使用条件:

1.要有一个大屏,或者配有两个屏幕工作,不然会很难受。
2.习惯xib布局,对xib布局情有独钟。

UIStoryBoard的特性:

.项目结构一目了然。界面布局更加便捷

UIStoryBoard--传值和逻辑处理

两个方法执行在页面跳转开始到下一个界面ViewDidLoad之前

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender NS_AVAILABLE_IOS(6_0); // Invoked immediately prior to initiating a segue. Return NO to prevent the segue from firing. The default implementation returns YES. This method is not invoked when -performSegueWithIdentifier:sender: is used.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender NS_AVAILABLE_IOS(5_0);
登录界面判断输入框内容是否合格处理。
1.新建storyBoard , 选择File - New - File - User Interface - storyBoard -输入“login” -- 生成了Login.storyboard,这是项目就有两个StoryBoard,一个Main.storyBoard,一个Login.storyBoard

2.新建LoginViewController和CompleteViewController(这个步骤免了),然后修改第二个ViewController 为LoginViewController,第三个ViewController 为CompleteViewController
3.把 LoginViewController.m中的#pragma mark - Navigation下面的方法解除注释,并添加-( BOOL)shouldPerformSegueWithIdentifier:( NSString *)identifier sender:( id)sender方法
4.添加逻辑处理和传值给CompleteViewController
-(BOOL)checkTextFild{
    if ([self.userTF.text isEqualToString:@""]) {
        return NO;
    }
    if ([self.passwordTF.text isEqualToString:@""]) {
        return NO;
    }
    return YES;
}
#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
{
    [segue.destinationViewController setUserNameString:self.userTF.text];
    [segue.destinationViewController setPasswordString:self.passwordTF.text];
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    return [self checkTextFild];
}

注:.shouldPerformSegueWithIdentifier 方法如果返回no,页面则不会跳转。

UIStoryBoard--UIStoryboardSegue

UIStoryboardSegue三个属性,下面会一一解释

@property (nonatomic, readonly) NSString *identifier;

@property (nonatomic,readonly)id sourceViewController;

@property (nonatomic,readonly)id destinationViewController;

1.首先什么是UIStoryboardSegue,就是ViewController 之间的“联线” ,

2.identifier 属性不用多说就是标示,设置的地方在storyboard中点击线条,然后在右面Attributes inspector中可看到有一个输入框对应identifier,一个选择框对用style(就是联接方式)

3.sourceViewController 和destinationViewController 分别指联接的前一个viewController 和后一个viewController

 应用实例代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSDate *object = _objects[indexPath.row];
        [[segue destinationViewController] setDetailItem:object];
    }else if([[segue identifier] isEqualToString:@"showOther"]){
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSDate *object = _objects[indexPath.row];
        [[segue destinationViewController] setOtherItem:object];
    }
}
这个逻辑结构参照第一张图,在tableview中定义两个cell,分别跳转到不同的viewCtroller,跳转时根据identifier调用对应方法。

Demo 下载地址:http://download.youkuaiyun.com/detail/wangtie_123/7576499

好了,以上就是UIStroryboard的基本应用。如果有什么没有表示清楚或者不解的地方,欢迎大家评论交流。








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值