![](/img/trans.png)
[英]Why is XCode archive acting different than XCode build/run on iPhone
[英]Different behavior in Build & Archive / TestFlight than building directly to device
我最近在我的应用程序中添加了在表格单元格中长按UILabel的功能,以使“复制”菜单出现,以便用户可以将文本复制到粘贴板。 无论是在模拟器中还是在直接构建到设备上时,它都能很好地工作。 但是,当我生成和存档(以便可以推送到TestFlight)时,该功能不起作用。
我尝试了这个Stack Overflow问题中的解决方案,但是它没有用(并且似乎不相关,因为我正在为iOS 5.0+开发)。 我在构建设置中将优化级别设置为None [-O0]
。
这是相关的代码(尽管我90%确信问题不是此代码,而是某些Xcode设置):
添加手势识别器:
UIGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPressForCopy:)];
[_postLabel addGestureRecognizer:longPress];
[self addSubview:_postLabel];
长按手柄
- (void)handleLongPressForCopy:(UILongPressGestureRecognizer *)recognizer {
switch (recognizer.state) {
case UIGestureRecognizerStateBegan:
NSAssert([self becomeFirstResponder], @"Sorry, UIMenuController will not work with %@ since it cannot become first responder", self);
UIMenuController *theMenu = [UIMenuController sharedMenuController];
CGRect displayRect = CGRectMake(_postLabel.frame.origin.x, _postLabel.frame.origin.y, 10, 0);
[theMenu setTargetRect:displayRect inView:self];
[theMenu setMenuVisible:YES animated:YES];
break;
default:
break;
}
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
return (action == @selector(copy:) );
}
就像我说的那样,它可以很好地在设备和模拟器中构建,而不仅仅是在Build&Archive之后。
在发布版本中未调用NSAssert
方法,因为-DNS_BLOCK_ASSERTIONS
标志已为发布版本启用。
在上面的代码中,我通过将[self becomeFirstResponder]
移动到其自己的行,将返回值分配给BOOL,然后在BOOL上调用NSAssert来解决了该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.