【问题标题】:Obj-C Delegate implementation in separate file单独文件中的 Obj-C 委托实现
【发布时间】:2015-08-26 08:47:21
【问题描述】:

我正在尝试清理我的代码。我的 ViewController 中有 3 个pickerView,我想在单独的文件中实现委托。

id imp = [[HourPickerImplementation alloc] init];
self.hourPicker.dataSource = imp;
self.hourPicker.delegate = imp;

HourPickerImplementation.h

@interface HourPickerImplementation : NSObject <UIPickerViewDataSource, UIPickerViewDelegate>

@end

HourPickerImplementation.m

@implementation HourPickerImplementation {
  NSMutableArray *_pickerData;
}

- (id)init {
  _pickerData = [[NSMutableArray alloc] initWithCapacity:25];
  for (int i = 0; i < 25; i++) {
    [_pickerData addObject:[NSString stringWithFormat:@"%d H.", i]];
  }
  return self;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
  return _pickerData.count;
}

- (UIView *)pickerView:(UIPickerView *)pickerView
            viewForRow:(NSInteger)row
          forComponent:(NSInteger)component
           reusingView:(UIView *)view {
  UIImage *img = [UIImage imageNamed:@"icon_lock.png"];
  UIImageView *temp = [[UIImageView alloc] initWithImage:img];
  temp.frame = CGRectMake(51, 20, 20, 20);

  UILabel *channelLabel =
      [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 60)];
  channelLabel.text =
      [NSString stringWithFormat:@"%@", [_pickerData objectAtIndex:row]];
  channelLabel.textColor = [UIColor whiteColor];
  channelLabel.backgroundColor = [UIColor clearColor];

  UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
  [tmpView insertSubview:temp atIndex:0];
  [tmpView insertSubview:channelLabel atIndex:1];

  return tmpView;
}

不幸的是它给了我这个错误:

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170099c80 V:[UIImageView:0x12751a110(55)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-08-26 10:30:30.489 bollywood[675:103276] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x17009a540 V:[UIImageView:0x12751c480(55)]>",
    "<NSLayoutConstraint:0x17009a590 V:[UIImageView:0x12751c5c0(50)]>",
    "<NSLayoutConstraint:0x17009a810 V:[UIImageView:0x12751c480]-(12)-[UIImageView:0x12751c5c0]>",
    "<NSLayoutConstraint:0x17009a900 UITableViewCellContentView:0x12751bcd0.bottomMargin == UIImageView:0x12751c5c0.bottom>",
    "<NSLayoutConstraint:0x17009aa40 UIImageView:0x12751c480.top == UITableViewCellContentView:0x12751bcd0.topMargin + 12>",
    "<NSLayoutConstraint:0x1740961c0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x12751bcd0(139)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17009a540 V:[UIImageView:0x12751c480(55)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

我做错了什么?它可以在一个文件中使用,但看起来很糟糕。

【问题讨论】:

  • 我很确定您发布的代码与您发布的错误无关。
  • 在评论此部分后://id imp = [[HourPickerImplementation alloc] init]; //self.hourPicker.dataSource = imp; //self.hourPicker.delegate = imp; 应用程序有效 - 但显然选择器中没有任何内容

标签: ios objective-c delegates uipickerview code-cleanup


【解决方案1】:

该错误暗示您的单元格中的约束无法满足,因为它们的组要求无法实现的布局。

查看输出,可能是以下行:

<NSLayoutConstraint:0x1740961c0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x12751bcd0(139)]>

此约束可能是由选取器视图根据您从 rowForHeightForComponent 返回的内容生成的。这个高度似乎是 139pts。

首先检查您的委托方法是否实际被调用。你说你拆分文件。猜测可能是系统将其设置为 139,因为它在移动后没有看到您的新委托代码。

您的其他约束似乎是 2 个高度为 55 和 50 的图像,12 之间的间隔和 12 的上边距。约束中的总固定高度为 117。我注意到您也在使用布局到边距。如果这不是您想要的,请将其更改为与实际顶部相反。

所以您的问题是单元格内容希望以固定大小/间距打包,但在设置为 139pts 时仍不能与顶部和底部对齐。

我的建议是检查您返回的行高的计算。

您可以通过将底部图像和内容视图底部对齐的底部约束更改为图像底部从内容视图底部固定 >=0 来解决约束问题.通过使此固定> = 0,约束将不再失败。不过,您会看到底部有一个间隙,您现在允许它处理代码中返回的高度之间的不匹配。

【讨论】:

  • 首先,他必须找到指定这些约束的代码。它也可以是一个 xib/故事板。绝对不是他发布的代码。
  • @Sulthan 是的,你是对的。但是我认为可能是通过将委托移动到其他地方,不会调用行的高度,并且系统正在使用默认值从其他地方填充空白。我的假设是基于这个问题,该问题表明这一直有效,直到代表被拆分为多个文件。
  • 在情节提要中删除我的 ViewController 中的所有约束之后。我遇到了同样的错误。
  • @Lau 首先检查您的委托高度方法是否实际被调用。我认为这是你真正的问题。其次,删除所有约束将无济于事,因为您现在没有布局。只需将底部约束更改为 >=0,这样当出现不匹配时,单元格就可以填充底部。但是首先要弄清楚为什么高度是错误的。 Mu 猜测是您的代表没有被调用。
  • 你是对的。这些委托方法不会被调用。
【解决方案2】:

解决方案:

问题出在这里:

- (void)viewDidLoad {
  [super viewDidLoad];

  id imp = [[HourPickerImplementation alloc] init];
  self.hourPicker.dataSource = imp;
  self.hourPicker.delegate = imp;
}

完成该方法后,我的委托被解除分配。我不得不将我的局部变量更改为全局变量。

- (void)viewDidLoad {
  [super viewDidLoad];

  _imp = [[HourPickerImplementation alloc] init];
  self.hourPicker.dataSource = _imp;
  self.hourPicker.delegate = _imp;
}

现在它可以工作了(感谢@Rory McKinnel)

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 2012-04-02
    相关资源
    最近更新 更多