ios-iOS app error - Can't add self as subview

问题描述

iOS app error - Can't add self as subview

iOS app error - Can't add self as subview
已被困扰要疯了,求助如何解决

解决方案

同一时间同时push多个controller在返回的时候会爆这样的错误,其本质的根源是push动画没有完成你就急着去push下一个controller。

解决的办法如下:

通过创建一个导航控制器的分类,将原有的push方法覆盖,再重写load方法,在里面exchange新的push和原始的push

iphone - iOS app error - Can't add self as subview - Stack Overflow

#import "UINavigationController+Consistent.h"

#import

/// This char is used to add storage for the

isPushingViewController property.

staticcharconst*constObjectTagKey="ObjectTag";

@interfaceUINavigationController()

@property(readwrite,getter = isViewTransitionInProgress) BOOL

viewTransitionInProgress;

@end
@implementationUINavigationController(Consistent)

-(void)setViewTransitionInProgress:(BOOL)property {

NSNumber *number = [NSNumbernumberWithBool:property];

objc_setAssociatedObject(self,ObjectTagKey, number , OBJC_ASSOCIATION_RETAIN);

}
-(BOOL)isViewTransitionInProgress {

NSNumber *number =objc_getAssociatedObject(self, ObjectTagKey);

return[number boolValue];

}
//注意上面是设置对象关联

#pragmamark -InterceptPop,Push,PopToRootVC

/// @name Intercept Pop, Push, PopToRootVC

  • (NSArray*)safePopToRootViewControllerAnimated:(BOOL)animated{

if(self.viewTransitionInProgress)returnnil;

if(animated){

self.viewTransitionInProgress =YES;

}
//-- This is not a recursion, due to method swizzling thecall below calls the originalmethod.

return[self

safePopToRootViewControllerAnimated:animated];

}

  • (NSArray*)safePopToViewController:(UIViewController*)viewController animated:(BOOL)animated {

if(self.viewTransitionInProgress)returnnil;

if(animated){

self.viewTransitionInProgress =YES;

}
//-- This is not a recursion, due to method swizzling thecall below calls the originalmethod.

return[selfsafePopToViewController:viewController animated:animated];

}

  • (UIViewController*)safePopViewControllerAnimated:(BOOL)animated{

if(self.viewTransitionInProgress)returnnil;

if(animated){

self.viewTransitionInProgress =YES;

}
return[selfsafePopViewControllerAnimated:animated];

}

  • (void)safePushViewController:(UIViewController*)viewController animated:(BOOL)animated {

self.delegate=self;

//-- If we are already pushing a view controller, we dont

push another one.

if(self.isViewTransitionInProgress == NO) {

//-- This is not a recursion, due to method swizzling thecall below calls the originalmethod.

[selfsafePushViewController:viewController animated:animated];

if(animated){

self.viewTransitionInProgress = YES;

}
}
}
// This is confirmed to be App Store safe.

// If you feel

uncomfortable to use Private API, you could also use the delegate method

navigationController:didShowViewController:animated:.

  • (void)safeDidShowViewController:(UIViewController*)viewController animated:(BOOL)animated {

//-- This is not a recursion. Due to method swizzling this

is calling the original method.

[selfsafeDidShowViewController:viewController animated:animated];

self.viewTransitionInProgress =NO;

}
// If the user doesnt complete the swipe-to-go-back gesture,

we need to intercept it and set the flag to NO again.

  • (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController*)viewController animated:(BOOL)animated {

id tc =navigationController.topViewController.transitionCoordinator;

[tcnotifyWhenInteractionEndsUsingBlock:^(id context) {

self.viewTransitionInProgress =NO;

//--Reenable swipe back gesture.

self.interactivePopGestureRecognizer.delegate= (id)viewController;

[self.interactivePopGestureRecognizer setEnabled:YES];

}];
//-- Method swizzling wont work in the case of a delegate

so:

//-- forward this method to the original delegate if there

is one different than ourselves.

if(navigationController.delegate!= self) {

[navigationController.delegatenavigationController:navigationController

willShowViewController:viewController

animated:animated];

}
}

  • (void)load {

//-- Exchange the original implementation with our custom

one.

method_exchangeImplementations(class_getInstanceMethod(self,@selector(pushViewController:animated:)),

class_getInstanceMethod(self,@selector(safePushViewController:animated:)));

method_exchangeImplementations(class_getInstanceMethod(self,@selector(didShowViewController:animated:)),

class_getInstanceMethod(self,@selector(safeDidShowViewController:animated:)));

method_exchangeImplementations(class_getInstanceMethod(self,@selector(popViewControllerAnimated:)),

class_getInstanceMethod(self,@selector(safePopViewControllerAnimated:)));

method_exchangeImplementations(class_getInstanceMethod(self,@selector(popToRootViewControllerAnimated:)),

class_getInstanceMethod(self,@selector(safePopToRootViewControllerAnimated:)));

method_exchangeImplementations(class_getInstanceMethod(self,@selector(popToViewController:animated:)),

class_getInstanceMethod(self,@selector(safePopToViewController:animated:)));

}

@end
//+ (void)load在类被加载的时候就会被执行,所以即使没有引入头文件或者主动调用他也会被执行。

解决方案二:

同一时间同时push多个controller在返回的时候会爆这样的错误,其本质的根源是push动画没有完成你就急着去push下一个controller。

解决的办法如下:

通过创建一个导航控制器的分类,将原有的push方法覆盖,再重写load方法,在里面exchange新的push和原始的push

iphone - iOS app error - Can't add self as subview - Stack Overflow

#import "UINavigationController+Consistent.h"

#import

/// This char is used to add storage for the

isPushingViewController property.

staticcharconst*constObjectTagKey="ObjectTag";

@interfaceUINavigationController()

@property(readwrite,getter = isViewTransitionInProgress) BOOL

viewTransitionInProgress;

@end
@implementationUINavigationController(Consistent)

-(void)setViewTransitionInProgress:(BOOL)property {

NSNumber *number = [NSNumbernumberWithBool:property];

objc_setAssociatedObject(self,ObjectTagKey, number , OBJC_ASSOCIATION_RETAIN);

}
-(BOOL)isViewTransitionInProgress {

NSNumber *number =objc_getAssociatedObject(self, ObjectTagKey);

return[number boolValue];

}
//注意上面是设置对象关联

#pragmamark -InterceptPop,Push,PopToRootVC

/// @name Intercept Pop, Push, PopToRootVC

  • (NSArray*)safePopToRootViewControllerAnimated:(BOOL)animated{

if(self.viewTransitionInProgress)returnnil;

if(animated){

self.viewTransitionInProgress =YES;

}
//-- This is not a recursion, due to method swizzling thecall below calls the originalmethod.

return[self

safePopToRootViewControllerAnimated:animated];

}

  • (NSArray*)safePopToViewController:(UIViewController*)viewController animated:(BOOL)animated {

if(self.viewTransitionInProgress)returnnil;

if(animated){

self.viewTransitionInProgress =YES;

}
//-- This is not a recursion, due to method swizzling thecall below calls the originalmethod.

return[selfsafePopToViewController:viewController animated:animated];

}

  • (UIViewController*)safePopViewControllerAnimated:(BOOL)animated{

if(self.viewTransitionInProgress)returnnil;

if(animated){

self.viewTransitionInProgress =YES;

}
return[selfsafePopViewControllerAnimated:animated];

}

  • (void)safePushViewController:(UIViewController*)viewController animated:(BOOL)animated {

self.delegate=self;

//-- If we are already pushing a view controller, we dont

push another one.

if(self.isViewTransitionInProgress == NO) {

//-- This is not a recursion, due to method swizzling thecall below calls the originalmethod.

[selfsafePushViewController:viewController animated:animated];

if(animated){

self.viewTransitionInProgress = YES;

}
}
}
// This is confirmed to be App Store safe.

// If you feel

uncomfortable to use Private API, you could also use the delegate method

navigationController:didShowViewController:animated:.

  • (void)safeDidShowViewController:(UIViewController*)viewController animated:(BOOL)animated {

//-- This is not a recursion. Due to method swizzling this

is calling the original method.

[selfsafeDidShowViewController:viewController animated:animated];

self.viewTransitionInProgress =NO;

}
// If the user doesnt complete the swipe-to-go-back gesture,

we need to intercept it and set the flag to NO again.

  • (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController*)viewController animated:(BOOL)animated {

id tc =navigationController.topViewController.transitionCoordinator;

[tcnotifyWhenInteractionEndsUsingBlock:^(id context) {

self.viewTransitionInProgress =NO;

//--Reenable swipe back gesture.

self.interactivePopGestureRecognizer.delegate= (id)viewController;

[self.interactivePopGestureRecognizer setEnabled:YES];

}];
//-- Method swizzling wont work in the case of a delegate

so:

//-- forward this method to the original delegate if there

is one different than ourselves.

if(navigationController.delegate!= self) {

[navigationController.delegatenavigationController:navigationController

willShowViewController:viewController

animated:animated];

}
}

  • (void)load {

//-- Exchange the original implementation with our custom

one.

method_exchangeImplementations(class_getInstanceMethod(self,@selector(pushViewController:animated:)),

class_getInstanceMethod(self,@selector(safePushViewController:animated:)));

method_exchangeImplementations(class_getInstanceMethod(self,@selector(didShowViewController:animated:)),

class_getInstanceMethod(self,@selector(safeDidShowViewController:animated:)));

method_exchangeImplementations(class_getInstanceMethod(self,@selector(popViewControllerAnimated:)),

class_getInstanceMethod(self,@selector(safePopViewControllerAnimated:)));

method_exchangeImplementations(class_getInstanceMethod(self,@selector(popToRootViewControllerAnimated:)),

class_getInstanceMethod(self,@selector(safePopToRootViewControllerAnimated:)));

method_exchangeImplementations(class_getInstanceMethod(self,@selector(popToViewController:animated:)),

class_getInstanceMethod(self,@selector(safePopToViewController:animated:)));

}

@end
//+ (void)load在类被加载的时候就会被执行,所以即使没有引入头文件或者主动调用他也会被执行。

解决方案三:

http://stackoverflow.com/questions/22421845/ios-app-crash-on-multiple-segue-at-the-same-time-going-to-other-segue-while-o/22928000#22928000

http://stackoverflow.com/questions/19560198/ios-app-err......
答案就在这里:iOS app error - Can't add self as subview

解决方案四:

同求答案

时间: 2024-09-15 03:46:13

ios-iOS app error - Can't add self as subview的相关文章

ios 构建版本-iOS 构建版本 ERROR ITMS-90207: "Invalid Bundle. 。。。

问题描述 iOS 构建版本 ERROR ITMS-90207: "Invalid Bundle. ... 我是用Xcode7.1 构建版本 错误ERROR ITMS-90207: "Invalid Bundle. The bundle at 'JiJie.app' does not contain a bundle executable." 使用Xcode6.4 没有出现的但传上去 一直显示 "正在处理" 求大神搭救???? 解决方案 加qq说吧.1700

ios 构建版本错误-ios 构建版本 ERROR ITMS-90529 90534 90207

问题描述 ios 构建版本 ERROR ITMS-90529 90534 90207 ERROR ITMS-90529: "Invalid package. Applications built with sdk 9.0 or later must be packaged as proper IPA files." ERROR ITMS-90534: "Invalid Toolchain. New apps and app updates must be built with

iOS 的 APP 在系统中如何适应 iPhone 5s/6/6 Plus 三种屏幕的尺寸?

初代iPhone 2007年,初代iPhone发布,屏幕的宽高是 320 x 480 像素.下文也是按照宽度,高度的顺序排列.这个分辨率一直到iPhone 3GS也保持不变. 那时编写iOS的App(应用程序),只支持绝对定位.比如一个按钮(x, y, width, height) = (20, 30, 40, 50),就表示它的宽度是40像素,高度是50像素,放在(20, 30)像素的位置. iPhone 4 2010年,iPhone 4发布,率先采用Retina显示屏,在屏幕的物理尺寸不变的

开发一个ios的app采用的技术架构是什么呀

问题描述 开发一个ios的app采用的技术架构是什么呀 是传统的分布式系统么?阿里云提出的云端开发一个app属于哪种方式呢,与传统的开发有什么不同. 解决方案 开发一个ios程序一般使用C/S构架,属于分布式架构的一种.也就是使用云+端的模式.阿里云提供的有PaaS和IaaS服务,这样比较传统开发,节约了总体拥有成本,简化了开发和部署. 解决方案二: 不自己写还能自动生成么- -?

求含有单元测试代码的ios版app

问题描述 求含有单元测试代码的ios版app 最近在学着写单元测试,终于摸到了xcTest的边,但是对于一个从来没有写过测试代码的人来说,突然写测试,有一种不知道如何下手的感觉 有人有iOS端的写了单元测试的项目吗,我想看看前辈们是怎么写测试的,好有个思路. 邮箱:huipaodetuzi@163.com 急,在线等.......

cocos2d-x如何将windows编好的程序生成ios平台app

问题描述 cocos2d-x如何将windows编好的程序生成ios平台app 如题,怎么跨平台,将windows编好的程序生成ios平台app 解决方案 不太懂,帮顶!不过据我所知,你是需要把源代码放到Mac里面,然后用Xcode重新编译一下的.大的地方不用改,可能需要修改一下链接库之类的东西,具体看你编译时遇到的问题了! 解决方案二: windows编好的程序 是什么语言编写的? 肯定不能直接放到xcode里面编译的吧? 可以考虑用Qt

“爱奇艺热聊”正式推出iOS平台App

8月6日,爱奇艺视频社交APP"爱奇艺热聊"正式推出iOS平台App,该产品是继奇谈和啪啪奇之后,爱奇艺在视频社交领域推出的又一款重要产品.用户可以通过"爱奇艺热聊"找到和自己有着同样观影兴趣的人,并通过文字.语音.投票等多种互动方式分享交流自己的看法.     过去,人们更习惯于和朋友面对面讨论自己感兴趣的影视节目,随着互联网的发展,这种讨论的核心阵地从线下转向线上,讨论的对象也从原来的周边好友变成了网络上的陌生同好.据悉,"爱奇艺热聊"从今年

Google Babel for iOS版或命名iOS Babel App

&http://www.aliyun.com/zixun/aggregation/37954.html">nbsp;   在I/O大会发布之前曾有谣传称Google将会整合旗下的聊天应用,推出 一款研发代号为"Babel"的统一界面信息服务应用, 不过在 大会上我们谷歌并未提到任何这个名词, 而是用Hangouts来整合旗下服务,不过最近又有 新的证据显示Google可能会在未来发布这样一款iOS应用,可以看到iOS平台上的Hangouts应用实际名称为&quo

Appium+python自动化20-查看iOS上app元素属性

前言 学UI自动化首先就是定位页面元素,玩过android版的appium小伙伴应该都知道,appium的windows版自带的Inspector可以定位app上的元素 Mac版的appium1.6的版本在UI上有了很大的改变,本篇详细解决如何使用appium1.6定位iOS的app页面元素. 一.Start New Session 1.启动appium后点Start New Session 2.打开后看到如下界面,刚开始看到这个界面,我也比较懵. 二.Desired Capabilities