草庐IT

self-referencing

全部标签

ios - 设置 self.delegate = self 是不是糟糕的设计

我有一个UIViewController子类(比如MyViewController)。MyViewController.h@protocolTargetChangedDelegate-(void)targetChanged;@end@interfaceMyViewController@property(weak)idtargetChangedDelegate;-(void)doSomethingOnYourOwn;@endMyViewController.m@implementationMyViewController-(void)doSomethingOnYourOwn{//DOso

iphone - 在 iOS 上,如果 View Controller 还没有 View ,为什么 NSLog( @"self.view is %p", self.view) 会崩溃?

如果在Xcode4.3.2和AppDelegate.m中使用EmptyApp模板创建新的iOS项目:self.window.rootViewController=[[FooViewControlleralloc]init];在FooViewController的viewDidLoad中,如下:NSLog(@"self.viewis%p",self.view);NSLog(@"self.viewis%@",self.view);将打印出View,因此看起来默认的loadView将实例化一个View并将其分配给self.view。因此,如果我用一个全空的方法覆盖loadView,并注释掉上

ios - 核心数据 : How to create a Managed Object without referencing the entity by string

为了提高我的代码的可读性,我一直在为我的实体生成核心数据类,这样我就可以使用getter和setter(我个人使用点表示法)而不是setValue:forKey:来访问它们>例如,代替[aDateObjectsetValue:nameStringforKey:@"name"];我将使用:aDateObject.name=nameString;我想知道是否有一种等效的方法可以将新的托管对象插入到托管对象上下文中。例如,我目前使用以下内容创建了一个新的DateTimeFlightDataType:aDateObject=(DateTimeFlightDataType*)[NSEntityD

ios - 为什么设备旋转时 self.view.frame 和 self.view.bounds 不同?

我想在设备旋转时更改一些布局。所以我实现了-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientationduration:(NSTimeInterval)duration方法来完成这项工作。但是我意识到当这个方法被调用时self.view.frame和self.view.bounds是不同的。self.view.bounds.size是正确的,self.view.frame.size似乎仍然没有旋转。例如,我创建了一个空的singleViewProject,实

objective-c - Objective-C中调用指定初始化器后如何进行附加初始化? ( self = [ self ...)

假设我有一个指定的初始化程序,它会像这样进行一些初始化:-(id)initWithBlah:(NSString*)arg1otherBlah:(NSArray*)arg2{if(self=[superinit]){...}returnself;}我有另一个初始化器需要调用它,然后执行一些其他设置任务:-(id)initWithSomeOtherBlah:(void*)creativeArg{//Isthisright?Itseemstocompileandrunasexpected,butfeelswrongself=[selfinitWithBlah:nilotherBlah:nil

objective-c - 有没有办法从应用程序本身到 'self-destruct'?

我想要从内部卸载应用程序的能力。示例:弹出UIAlertView并提示“此应用程序已过期”。用户点击“确定”,然后应用程序继续关闭并将其自身从设备中删除。有什么办法吗? 最佳答案 我认为没有办法从手机本身删除该应用程序。但是,您可以使用代码终止应用程序的执行,exit(0);也不鼓励这样做,因为您不应该在用户不知情的情况下突然退出应用程序。但是,如果您显示警报并退出该应用程序,那没关系,并且确实有必要退出该应用程序。 关于objective-c-有没有办法从应用程序本身到'self-de

ios - 在 block 中使用局部变量名称 "self"是否正确?

我发现构造__strongtypeof(self)self=weakSelf。它允许删除NSAssert宏self捕获,但我怀疑以这种方式使用它是否正确?__weaktypeof(self)weakSelf=self;self.signupBlock=^{__strongtypeof(self)self=weakSelf;NSLog(@"%d",self.property)NSAssert((self.property>5),@"Somemessage");}请多多指教。对不起,我不得不先说使用__strongtypeof(self)strongSelf=weakSelf;构造结果以w

ios - self.viewController.webView stringByEvaluatingJavaScriptFromString 不起作用

NSString*jsString=[NSStringstringWithFormat:@"alert('ok');"];[self.viewController.webViewstringByEvaluatingJavaScriptFromString:jsString];我在cordovaappdelegate.m中写的! 最佳答案 在cordova-ios@3.x上你应该能够做到:[self.webViewstringByEvaluatingJavaScriptFromString:jsString];在cordova-ios

iphone - 当 App 进入前台时调用 self.viewDidAppear

我想让我的应用在用户将应用带到前台时再次调用viewDidAppear。-(void)appReturnsActive{//THISISTHEBITTHATDOESNTWORK,BUT[self.viewDidLoad]DOESWORK[self.viewDidAppear];}我正在像这样在我的viewDidAppear方法中创建appReturnsActive。它运作良好:[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(appReturnsActive)name:UIApplication

ios - self 弱的 NSTimer;为什么不调用 dealloc?

考虑具有强(或弱,相同)NSTimer属性的ViewController:__weak__typeof(self)ws=self;self.timer=[NSTimerscheduledTimerWithTimeInterval:2target:wsselector:@selector(timerTigger:)userInfo:nilrepeats:YES];但为什么这个ViewController不调用dealloc方法,无论我通过strong还是weak引用到self?详细代码如下:#import"SecondViewController.h"@interfaceSecondVi