小编Ser*_*nko的帖子

为什么我必须在Apple sdk中没有任何IBOutlets时将IBOutlet标识符添加到我的委托?

我创建了以下自定义视图:

@interface TNGalleryView : UIView

@property (assign, nonatomic) id <TNGalleryViewDelegate> delegate;
@property (assign, nonatomic) id <TNGalleryViewDataSource> dataSource;

@end
Run Code Online (Sandbox Code Playgroud)

我可以在代码中分配它的委托,但是当我想在IB中分配委托时,我不能.我必须将我的代码更改为:

@interface TNGalleryView : UIView

@property (assign, nonatomic) IBOutlet id <TNGalleryViewDelegate> delegate;
@property (assign, nonatomic) IBOutlet id <TNGalleryViewDataSource> dataSource;

@end
Run Code Online (Sandbox Code Playgroud)

然后我可以在IB中分配delegate和dataSource.

为什么我想在IB中分配delegate和dataSource我必须添加IBOutlet标识符,但在apple sdk中没有任何IBOutlets?例如,UICollectionView声明的这一部分:

NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionView : UIScrollView

- (id)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout; // the designated initializer

@property (nonatomic, retain) UICollectionViewLayout *collectionViewLayout;
@property (nonatomic, assign) id <UICollectionViewDelegate> delegate;
@property (nonatomic, assign) id <UICollectionViewDataSource> dataSource;
Run Code Online (Sandbox Code Playgroud)

没有任何IBOutlet,但我可以在IB中分配dataSource并委托给它.

objective-c ios

7
推荐指数
1
解决办法
1537
查看次数

将子控制器添加到表视图单元时,不会调用viewWillAppear

当我将子视图控制器添加到表视图单元格时,它看起来像子视图控制器的viewWillAppear没有被调用,只有viewDidAppear.

表视图控制器方法:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("ShopInfoTableViewCell", forIndexPath: indexPath) as! ShopInfoTableViewCell
    self.addChildViewController(self.shopInfoViewController, toView: cell.containerView)
    return cell
}
Run Code Online (Sandbox Code Playgroud)

查看控制器类别方法:

- (void)addChildViewController:(UIViewController *)childController toView:(UIView *)view
{
    [self addChildViewController:childController];
    [view addSubview:childController.view];
    [childController didMoveToParentViewController:self];

    [childController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_top);
        make.bottom.equalTo(view.mas_bottom);
        make.left.equalTo(view.mas_left);
        make.right.equalTo(view.mas_right);
    }];
}
Run Code Online (Sandbox Code Playgroud)

任何想法为什么会发生?

iphone uiviewcontroller ios

2
推荐指数
1
解决办法
5138
查看次数

为什么编译器不会在[NSObject init]上返回错误

当我尝试编译以下代码时:

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        [NSObject init];
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器让它构建,然后应用程序在运行时崩溃,但有以下异常:

2017-08-14 14:56:07.937859-0700 NSObjectInit[30512:11241814] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSObject<0x7fff9fd83140> init]: cannot init a class object.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff818e32cb __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fff966f348d objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff8196517f +[NSObject(NSObject) init] + 127
    3   NSObjectInit                        0x0000000100000f2d main + 61
    4   libdyld.dylib                       0x00007fff96fd9235 start + 1
    5   ???                                 0x0000000000000001 0x0 + …
Run Code Online (Sandbox Code Playgroud)

xcode objective-c foundation nsobject

1
推荐指数
1
解决办法
81
查看次数

如何使用ARC释放块

说明:

我将块传递给异步方法,并在操作完成时调用它.我想在操作完成之前拒绝调用块.但是如果我将nil分配给我的类中的块变量,那么无论如何都要调用它.我调试了它,我看到如果我将nil分配给块变量1,则变量2不是nil.下面的代码说明了这一点:

void (^d1)(NSArray *data) = ^(NSArray *data) {};

void (__weak^d2)(NSArray *data) = d1;

d1 = nil;
Run Code Online (Sandbox Code Playgroud)

输出:

(lldb) po d1
<__NSGlobalBlock__: 0x9c22b8>]
(lldb) po d2
<__NSGlobalBlock__: 0x9c22b8>
(lldb) po d1
<nil>
(lldb) po d2
<__NSGlobalBlock__: 0x9c22b8>
Run Code Online (Sandbox Code Playgroud)

问题:

为什么阻止d2不是零?是按值复制而不是复制为指针?

objective-c ios objective-c-blocks

0
推荐指数
1
解决办法
906
查看次数