KVO, 即键值观察,提供了一种让一个对象监听另一个对象的特定属性变化的机制。这在 MVC 的 Model 层 和 Controller 层间通信十分有用。通常情况下,Controller 会监听 Model 对象的属性变化,或者 View 对象会通过 Controller 来监听 Model 对象的属性变化。除此之外,在 Model 对象需要感知其依赖值的改变的时候,该 Model 对象也可以监听其他 Model 对象或者其自身的属性变化。

监听属性变化需要以下几步:

  1. 使用函数 addObserver:forKeyPath:options:context: 建立观察者和被观察者对象之间的连接,这种连接不是建立在这两个类之间,而是两个对象实例之间。
  2. 为了响应被观察者对象的变化通知,观察者必须实现 observeValueForKeyPath:ofObject:change:context: 方法,该方法定义了观察者是如何对被观察者的变化做出响应的。
  3. 当被观察的属性发生变化时,observeValueForKeyPath:ofObject:change:context: 方法会自动调用。
  4. 调用 – removeObserver:forKeyPath:context: 取消注册。
Continue reading

在项目,可能会有需求需要监听 NSMutableArray 的变化,例如在可变数组中加入、删除或者替换了元素,我们需要根据这些变化来更新UI或者做其他操作。

那么如何来监听呢?

方法1,使用 mutableArrayValueForKey: 代理,这样,我们在获取定义的数组属性时不再使用其 getter 方法,而是通过代理方法获取数组属性后,再对数组进行增删改的操作。这是最简单高效的方法,使用示例如下:

Continue reading

使用 Storyboard 时,通常情况下,可能你不会去关心 NSLayoutConstraint 的 priority 属性,使用 Storyboard 提供的默认值就能达到要求。但是最近遇到对 UILabel 的如下布局要求时,就需要涉及对 priority 的修改。

NSLayoutConstraint priority 示例
NSLayoutConstraint priority 示例

如上所示,我们期望 UILabel 显示字符串较短时,以内容的多少来决定其宽度;当其显示的字符串较长时,截断其尾部,且 UILabel 的 Trailing 距离 UIButton 的 Leading 大于等于30,UIButton的宽度由其title内容决定。在 Storyboard 将 UILabel 和 UIButton 设置好约束,不改变约束的 priority 的前提下,在 Xcode6.4 的环境下会出现约束错误提示。你可先考虑为什么会出现约束错误以及如何解决这种错误。

Continue reading

iOS 中可获取当前网络接口的 SSIDBSSID 信息,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
NSString *ssid = @"";
NSString *bssid = @"";
NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
NSDictionary *info;
if ([ifs count] > 0) {
for (NSString *ifname in ifs) {
info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifname);

NSString *ssidKey = (__bridge_transfer NSString *)kCNNetworkInfoKeySSID;
NSString *bssidKey = (__bridge_transfer NSString *)kCNNetworkInfoKeyBSSID;
if (info) {
ssid = info[ssidKey];
bssid = info[bssidKey];
NSLog(@"ssid: %@", ssid);
NSLog(@"bssid: %@", bssid);
}
}
}
Continue reading

现在iOS新建项目工程时,window 默认情况下是以 Main.storyboard 中 initial view controller, 在项目中,如果需要通过代码控制 window 的 rootViewController , 可以通过如下方法:

1
2
3
4
5
6
7
8
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
RootViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"RootViewController"];
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
Continue reading
  • page 1 of 1
Author's picture

CaryaLiu

@Chengdu


iOS Developer


Chengdu