- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我将 UIViewController
子类化为 STViewController
并注意到从 STViewController
继承的类有它们的 viewDidLoad
方法被重复调用.最终导致应用程序崩溃。 STViewController
在这一点上基本上是一个空白的实现。我正在子类化,如下所示:
#import "STViewController.h"
@interface WelcomeViewController : STViewController {
STViewController.h
#import <UIKit/UIKit.h>
@interface STViewController : UIViewController
{
}
@end
STViewController.m
#import "STViewController.h"
@implementation STViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView
{
// Implement loadView to create a view hierarchy programmatically, without using a nib.
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
来自 WelcomeViewController.m 的 viewDidLoad()
- (void)viewDidLoad
{
[super viewDidLoad];
// hide the buttons
[[self signUp] setHidden: YES];
[[self logIn] setHidden: YES];
}
最佳答案
您正在覆盖 loadView
,但您的实现是空的,并且您没有分配 View 。删除 loadView
覆盖。
来自 UIViewController Class Reference (强调我的):
You should never call this method directly. The view controller calls this method when the view property is requested but is currently nil. If you create your views manually, you must override this method and use it to create your views. If you use Interface Builder to create your views and initialize the view controller—that is, you initialize the view using the initWithNibName:bundle: method, set the nibName and nibBundle properties directly, or create both your views and view controller in Interface Builder—then you must not override this method.
The default implementation of this method looks for valid nib information and uses that information to load the associated nib file. If no nib information is specified, the default implementation creates a plain UIView object and makes it the main view.
If you override this method in order to create your views manually, you should do so and assign the root view of your hierarchy to the view property. (The views you create should be unique instances and should not be shared with any other view controller object.) Your custom implementation of this method should not call super.
关于objective-c - 子类化 UIViewController,viewDidLoad 反复调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8866573/
我正在学习 objective-c 教程,并注意到 viewDidLoad 中的代码放在 super viewDidLoad 下,而不是第一次调用 viewDidLoad。 代码放在viewDidLo
在Apple的scrollView示例中,他们没有这样称呼。我一直认为这是必须的。我为什么要这样调用它? 最佳答案 如果您要重写该方法,您仍然应该在 super 中调用该方法。即使父类(super c
这个问题已经有答案了: Why/when do we have to call super.ViewDidLoad? (6 个回答) 已关闭 4 年前。 override func viewDidLo
大家好,我有一个双 View Controller 。 我的 firstviewcontroller 有一个按钮,这个按钮发送 NSNotification,secontviewController
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Do I always have to call [super viewDidLoad] in the -v
我有一个关于子类化的问题。 我从我的第一个 View 开始:在我的 .h 文件中: @interface viewAController : UIViewController 在我的 .m 文件中:
我读到一篇文章说,当创建 UIViewController 类时,属性需要为零,然后 Storyboard 或在调用 viewDidLoad 时将其添加到值中。 它表示在构造对象时也无法初始化属性。
这是我当前的代码。我什至没有通过 viewDidLoad 方法覆盖,因为在它说 super viewdidload 的那一行它抛出错误 @interface for uitableview 为选择器
import UIKit class SecondViewController: UIViewController { @IBOutlet weak var labelA: UILabel!
我需要设置和存储一些可用于 View 的 NSUserDefaults 数据。这不适用于 viewDidLoad。 这可以做到吗? 在 viewDidLoad 之前我可以使用什么方法? 你会推荐什么?
当我向主视图 Controller 中的 viewDidLoad 添加方法时,我的 iPhone 应用程序崩溃了: #import "dbQuestionGetterViewController.h"
当用户切换到另一个程序然后再次返回时,原始程序的 View 将被另一个程序的新 View 替换。那么当用户切换回原来的程序时,viewDidLoad会被第二次调用吗? 我问这个是因为如果是这种情况,那
全部, 我正在从我的 applicationDidFinishLaunching 方法创建一个 windowController ... (void)applicationDidFinishLaunc
这个问题已经有答案了: Looking to understand the iOS UIViewController lifecycle (11 个回答) iPhone SDK: what is th
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 5年前关闭。 Improve this
我知道viewDidLoad在 UIViewController 期间可能会多次调用方法的生命周期。但这怎么可能?如何让它多次调用而不是直接调用它?我试过这样做: UIView *view = [
我在使用 iOS 时遇到了一点问题。我使用协议(protocol)在两个 View Controller 之间来回传递数据并手动切换 View 。我的问题是,当我关闭顶 View 时,不再调用底 Vi
我正在开发一款新应用程序,该应用程序主要用于组织网络内容中的信息。它将是一款用于监控道琼斯指数股息 yield 的金融应用程序。为此,需要执行几个步骤,但我现在正在执行第一步。我想选取道琼斯指数中的
我需要采用 viewDidLoad 方法中填充的变量来显示在连接到自定义单元格的标签上。我想做的是: 查找数据库中存储的用户盒子中的 SKU 使用SKU查找数据库中存储的产品详细信息 将产品详细信息存
我正在 viewDidLoad 中使用异步任务函数,根据返回的响应,我正在更改 View (颜色、文本...),但我有“错误”: 有时,当我更改 View 并返回 View 时,我的 View 背景没
我是一名优秀的程序员,十分优秀!