美文网首页iOS开发记录程序员iOS 开发
10张图告诉你Storyboard的强大!

10张图告诉你Storyboard的强大!

作者: dongyehu | 来源:发表于2016-04-22 18:14 被阅读1010次

Storyboard对TableView的强力支持

  1. 为tableview添加headview,footview
record1.gif
运行图
  1. Storyboard直接关联cell

创建cell文件,并与storyboard视图cell关联,并设置复用标识


record2.gif

关联属性


record3.gif
Simulator Screen Shot 2016年4月22日 下午3.53.30.png
  1. 在ViewController里面添加TableView

设置TableView的代理


47D183E5-D62D-4928-84BD-D14AEF9809E5.png

在UINavigationcontroller 的RootViewController的View视图只添加TableView 有 留白 的问题


record4.gif
B2E6E945-3770-427D-8652-07EA74E1DB76.png

解决办法:在添加TableView之前添加一个任意视图

B5DF0F4C-BA8E-4E4F-8EE4-C0AC4B94BE12.png

XIB直接关联到ViewController

record5.gif

在Storyboard里控制返回

不传数据返回


record6.gif

不用协议代理携带数据返回,(注意gif中关联返回方法的地方)


A.png
B.png
record7.gif

在Storyboard里控制预览你的代码

IBInspectable
在OC中使用IBInspectable,在swift中使用@IBInspectable。用它修饰的属性或者实例变量,可以显示在xib中的属性栏中

IBInspectable.png
@interface ViewController : UIViewController

//gj_testFlag用IBInspectable修饰后,就能在xib中看到这个属性了,当然也可以用xib进行赋    值了
@property (assign, nonatomic) IBInspectable BOOL gj_testFlag;

@end

IB_DESIGNABLE
在OC中将IB_DESIGNABLE写在@implementation前,在swift中将@IBDesignable写在class前
它的作用是可以在不运行的情况下把你的代码显示在xib或SB文件中。

  1. 这是一个针对UI显示的功能,所以只能是在UIView及其子类或者NSView及其子类上生效。
  2. 要想使IBDesignable起作用必须把代码写在drawRect里才能显示,同样的代码,我写在了awakeFromNib里就不会再xib中看出效果,只有写在了drawRect才可以。
IB_DESIGNABLE.png
IB_DESIGNABLE
@implementation TestView

- (void)drawRect:(CGRect)rect {
UIBezierPath *firtPath = 
[UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 180, 180)];
CAShapeLayer *shapeL = [CAShapeLayer layer];
shapeL.lineWidth = 20;
shapeL.path =firtPath.CGPath;
shapeL.strokeStart = 0;
shapeL.strokeEnd = 1;
shapeL.strokeColor = [UIColor yellowColor].CGColor;
shapeL.fillColor = [UIColor clearColor].CGColor;
[self.layer addSublayer:shapeL];
self.layer.cornerRadius = 30;
self.layer.masksToBounds = YES;
}

@end

在Storyboard国际化

add.png add1.png add2.png add3.png

在Storyboard的Runtime Attribute

Runtime1.png
Runtime2.png

在Storyboard关联NSObject

1.我们先建立一个Person类,继承自NSObject。
2.我们在Person类中添加一个简单的测试方法:
- (IBAction)sayHello:(id)sender { NSLog(@"hello person");}

3.我们在一个VC中添加这样的代码:
#import "Person.h"
@interface ViewController : UIViewController
@property (nonatomic, strong) IBOutlet Person *aPerson;
@end
4.我们找到ViewController对应的xib或SB文件,拖入一个button,把这个button与Person类
中的sayHello:函数“连线”,你发现根本连接不上
5.在右边栏中找到Object这个对象,拖动它到左侧边栏中,把Object对象的class设置为 Person,大功告成!

show.gif

如果有些地方你不是很明白,这里有更详细的教程!

相关文章

网友评论

    本文标题:10张图告诉你Storyboard的强大!

    本文链接:https://www.haomeiwen.com/subject/eyrxrttx.html