何时调用[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
最满意答案
好的,我测试了它,即使有一个以上的“主动”类被分配给一个元素,这也是可行的:
$('[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.
相关问答
更多-
删除所有类,除了一个(Remove all classes except one)[2021-02-09]
您可以通过以下两个步骤进行操作:您可以使用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 ...
-
用jquery匹配类(Matching classes with jquery)[2023-06-16]
$('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列表中选择一个元素; “索引”功能 ... -
jquery删除匹配的类?(jquery remove matching classes?)[2023-06-16]
好的,我测试了它,即使有一个以上的“主动”类被分配给一个元素,这也是可行的: $('[class*="active"]').removeClass(function(i, c) { return c.match(/active\d+/g).join(" "); }); 'i'是匹配元素的索引,'c'是匹配元素的类属性的值,因此您不需要再次“询问”它。 $(“...”)。removeClass()可以删除由该值指定的所有类,所以如果有多于一个“active-something”类分配给此元素,我们将返回 ... -
使用jQuery从匹配特定格式的元素中删除所有类(Remove all class from an element matching a specific format, using jQuery)[2022-04-14]
好吧,我想你可以直接搞乱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 ... -
使用jQuery / JavaScript动态检测匹配的类名(Detecting matching class names dynamically with jQuery/JavaScript)[2022-03-01]
假设你需要: 检查两个项目是否具有.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(){ $('. ...
-
标识子元素的类并删除所有匹配类的属性(Identify class of child element and remove an attribute of all matching classes)[2022-08-14]
代码应该是不言自明的: $('.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. ...