首页 \ 问答 \ 何时调用[super viewDidLoad]和[super viewDIdUnload]?(When to call [super viewDidLoad] and [super viewDIdUnload]?)

何时调用[super viewDidLoad]和[super viewDIdUnload]?(When to call [super viewDidLoad] and [super viewDIdUnload]?)

在我的大部分代码中,我有以下设置viewDidLoad和viewDidUnload:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //do stuff...
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    //do stuff...
}

然而,我想知道...当你调用viewDidLoad和viewDidUnload时,它有什么关系吗? 每个人应该在我“做什么”之前或之后?

换句话说,每个人应该在方法的开始还是结束?

编辑:为了进一步复杂化,这是苹果的默认viewDidUnload方法,“似乎”建议首先调用[super viewDidUnload] ...

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

In most of my code, I have the following setup for viewDidLoad and viewDidUnload:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //do stuff...
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    //do stuff...
}

However, I got to wondering... does it matter when you call viewDidLoad and viewDidUnload? Should each one be before or after I "do stuff"?

In other words, should each one be at the start or end of the method?

Edit: To further complicate things, this is Apple's default viewDidUnload method, which "seems" to suggest [super viewDidUnload] be called first...

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

原文:https://stackoverflow.com/questions/12847273
更新时间:2023-01-29 08:01

最满意答案

好的,我测试了它,即使有一个以上的“主动”类被分配给一个元素,这也是可行的:

$('[class*="active"]').removeClass(function(i, c) {
  return c.match(/active\d+/g).join(" ");
});

'i'是匹配元素的索引,'c'是匹配元素的类属性的值,因此您不需要再次“询问”它。 $(“...”)。removeClass()可以删除由该值指定的所有类,所以如果有多于一个“active-something”类分配给此元素,我们将返回所有匹配的匹配项在正则表达式的末尾使用'g'选项),然后连接它,以便实际的removeClass函数可以相应地处理它。


OK, I tested it and this definitely works even if there are more than one "active-something" classes assigned to one element:

$('[class*="active"]').removeClass(function(i, c) {
  return c.match(/active\d+/g).join(" ");
});

The 'i' is the index of matched element and 'c' is the value of the class attribute for the matched element so you don't need to "ask" for it again. $("...").removeClass() can remove all classes specified by the value so if there are more than one "active-something" classes assigned to this element we're returning all the occurences from the call to match (using the 'g' option at the end of the regular expression) and then concatenating it so that the actual removeClass function then can process it accordingly.

相关问答

更多
  • 您可以通过以下两个步骤进行操作:您可以使用attr通过用所需的类覆盖所有类值来重置整个值: jQuery('#container div.cleanstate').attr('class', 'cleanstate'); 示例: http : //jsfiddle.net/jtmKK/1/ Instead of doing it in 2 steps, you could just reset the entire value at once with attr by overwriting all of ...
  • $("#item").removeClass(); 调用没有参数的removeClass将会删除所有项目的类。 您也可以使用(但不一定推荐, 正确的方式是上面的): $("#item").removeAttr('class'); $("#item").attr('class', ''); $('#item')[0].className = ''; 如果你没有jQuery,那么这将是你唯一的选择: document.getElementById('item').className = ''; $("#i ...
  • $('ul.list2 li:nth-child(' + $('ul.list1 li.current').index() + ')').addClass('current'); 或者你可以让它不那么icky: $('ul.list2 li').eq($('ul.list1 li.current').index()).addClass('current'); 第二个,我更喜欢,现在我可以看到他们两个:-)这里的技巧是: “eq”过滤器可以让你从一个基于索引的jQuery列表中选择一个元素; “索引”功能 ...
  • 好的,我测试了它,即使有一个以上的“主动”类被分配给一个元素,这也是可行的: $('[class*="active"]').removeClass(function(i, c) { return c.match(/active\d+/g).join(" "); }); 'i'是匹配元素的索引,'c'是匹配元素的类属性的值,因此您不需要再次“询问”它。 $(“...”)。removeClass()可以删除由该值指定的所有类,所以如果有多于一个“active-something”类分配给此元素,我们将返回 ...
  • 好吧,我想你可以直接搞乱class属性,就像这样: $('#item_3').attr('class', function() { return $.trim($(this).attr('class').replace(/child_\S+/g, '')); }); 基本上,您搜索以child_为前缀的类,替换为nothing,然后使用生成的字符串设置class属性。 正则表达式/child_\S+/g匹配child_后跟非空白字符,因为类名是空格分隔的。 jsFiddle预览 Well, I s ...
  • 假设你需要: 检查两个项目是否具有.active 并有相同的类(不包括flip和active ) if ($(".active").length === 2) { var class1 = (" " + $(".active:eq(0)").attr("class") + " ") .replace(" flip ", "") .replace(" active ", "") .replace(/^ +| +$/g, ""); var clas ...
  • 您可以通过将空格分隔开来一次删除多个类,如下所示: $('.clearbtn_1').click(function(){ $('.valid_div1').removeClass('valid_tick invalid_tick'); }); You can remove multiple classes at once by separating the classes by spaces like so : $('.clearbtn_1').click(function(){ $('. ...
  • 代码应该是不言自明的: $('.parent').click(function() { // Get all classes from child elements. var classes = $(this).children().map(function() { return this.className.split(/\s+/); }).get(); // Find any element having one of those classes, fi ...
  • 这是实现这一目标的一种可能方式。 jquery ui小部件引发了一个'create'事件,该事件在执行_create()和_init()方法之间触发: this._create(); this._trigger( "create" ); this._init(); 此事件是从基础对象Widget引发的,因此它可用于实现它的所有小部件。 'ui-corner-xxx'(和其他)的类通常在'_create()'方法中创建,因此您可以将事件处理程序绑定到窗口小部件的'create'选项以删除这些类。 就像是: ...
  • 这应该工作。 $('#siteFeelingBannar li').click(function(){ // Add the class to div and spans $(this).siblings().find('div').addClass('hidden'); $(this).siblings().find('span').addClass('autologinMargin'); // Remove the classes from div and span. ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。