self.window 与 _window 的区别关系

本文探讨了一个iOS应用启动时的问题,即按钮无法显示的情况。通过分析@property定义的变量及@synthesize的作用,找到了问题的根本原因,并给出了有效的解决办法。

遇到一个问题,在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 直接[self.windo addSubview:button];结果buttton没有显示出来,最后改成[_window addSubview:button],结果button显示,google了一下,大致是这样的。

首先debug发现Appdelegate的成员变量是这样的,自动出来了个_window。

查了下原因如下:

http://stackoverflow.com/questions/6130529/ios-xcode-4-properties-access

大致意思是这样的,

一是:@property定义的变量系统会生成对应的以''_"开头的变量名。重新定义 uiview测试如下图:


二是:可以在.m文件中@synthesize window=_window可以用此种形式覆盖(其实也能叫覆盖,只是没有好的词汇,其实在.m文件中自己是不用写@synthesize实现,应该是被系统默认实现了,所以自己再写,就姑且叫覆盖或者重写)系统默认@synthesize实现。然后在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 直接[self.windo addSubview:button],button也添加上去了,也是起作用。

最后最本质的区别是(个人认为)self.window 其实对getter方法的调用,其返回的是名叫window的变量,而从debug来看不存在这个window变量,不起作用也就很正常了。另外在.m重新@synthesize window=_window覆盖后,应该把_window赋值给window(水平有限只能这样理解),而_window则是直接调用,当然起作用。

其实这些都不重要,重要的是,我记下来了,解决了问题。其实底层的东西,对于闭源的ios来说,只能问那些苹果的大牛了。


以上只是个人观点。


def handle_new_windows(self, original_window): current_windows = self.driver.window_handles if len(current_windows) > 1: for window in current_windows: if window != original_window: self.driver.switch_to.window(window) self.driver.close() self.driver.switch_to.window(original_window) def click_navigation_bars(self, clicked_elements, depth=0, max_depth=5): if depth > max_depth: print("达到最大递归深度,停止点击导航栏") return original_window = self.driver.current_window_handle elements = self.get_navigation_elements() for element in elements: try: element_id = self.element_actions.get_element_identifier(element) if element_id in clicked_elements: continue if not element.is_displayed() or not element.is_enabled(): continue before_url = self.driver.current_url self.element_actions.scroll_to_element(element) self.element_actions.highlight_element(element) self.element_actions.safe_click(element) clicked_elements.add(element_id) print(f"点击导航栏元素: {element_id}") # 处理跳过指定url 测试时节约时间 if self.pass_handler(self.driver.current_url): continue self.element_actions.handle_alert() self.element_actions.handle_new_windows(original_window) time.sleep(0.5) self.element_actions.handle_input_fields() # 处理输入框 # 点击导航栏后检查是否有新非导航栏元素 self.click_all_clickable_elements(clicked_elements, depth=depth + 1) if self.driver.current_url != before_url: self.driver.back() time.sleep(0.5) except Exception as e: print(f"点击导航栏元素{element_id}失败: {e}") continue # elements = self.get_navigation_elements() print("导航栏元素已全部点击完毕") def click_all_clickable_elements(self, clicked_elements=None, depth=0, max_depth=5): if depth > max_depth: print("达到最大递归深度,停止点击") return if clicked_elements is None: clicked_elements = set() #点击导航元素 self.click_navigation_bars(clicked_elements) #点击非导航元素 original_url = self.driver.current_url original_window = self.driver.current_window_handle elements = self.element_actions.get_all_clickable_elements() for element in elements: try: element_id = self.element_actions.get_element_identifier(element) if element_id in clicked_elements: continue if self.element_actions.is_sensitive_element(element): clicked_elements.add(element_id) continue if not element.is_displayed() or not element.is_enabled(): continue self.element_actions.scroll_to_element(element) self.element_actions.highlight_element(element) print(f"点击: {element_id}元素") self.element_actions.safe_click(element) clicked_elements.add(element_id) # 处理跳过指定url 测试时节约时间 if self.pass_handler(self.driver.current_url): continue self.element_actions.handle_alert() self.element_actions.handle_new_windows(original_window) time.sleep(0.1) if self.driver.current_url != original_url: # 递归点击新页面 self.click_all_clickable_elements(clicked_elements, depth=depth + 1) self.driver.back() time.sleep(0.1) except Exception as e: print(f"点击失败: {e}") continue print(f"当前页面{self.driver.current_url}所有元素已点击完毕") 我对新窗口的处理是关闭,如果我需要进入新窗口进行点击操作,我该怎么改
08-29
我该怎么调用def handle_page_change(self): try: alert = WebDriverWait(self.driver, 2).until(EC.alert_is_present()) if alert: alert.accept() print("已处理弹窗") except TimeoutException: pass original_window = self.driver.current_window_handle windows = self.driver.window_handles if len(windows) > 1: for window in windows: if window != original_window: self.driver.switch_to.window(window) self.driver.close() self.driver.switch_to.window(original_window) 在下面这个函数(考虑origin_Window) def click_all_clickable_elements(self): try: elements_locator = (By.CSS_SELECTOR, SvelteKitAutomator.clickable_selectors) elements = WebDriverWait(self.driver, 10).until( EC.presence_of_all_elements_located(elements_locator) ) count = len(elements) print(f"初始找到 {count} 个可能可点击的元素") original_window = self.driver.current_window_handle original_url = self.driver.current_url for index in range(count): try: elements = self.driver.find_elements(*elements_locator) if index >= len(elements): print(f"元素列表已变化,跳过索引 {index}") continue element = elements[index] self.scroll_to_element(element) if element.is_displayed() and element.is_enabled(): element.click() print(f"已点击元素 {index + 1}/{count}") time.sleep(0.5) try: alert = WebDriverWait(self.driver, 3).until(EC.alert_is_present()) print("检测到弹窗,正在处理...") alert.accept() except TimeoutException: print("无弹窗") time.sleep(0.2) current_windows = self.driver.window_handles if len(current_windows) > 1: for window in current_windows: if window != original_window: self.driver.switch_to.window(window) self.driver.close() self.driver.switch_to.window(original_window) else: if self.driver.current_url != original_url: self.driver.back() WebDriverWait(self.driver, 10).until( EC.presence_of_all_elements_located(elements_locator) ) except Exception as e: print(f"点击元素 {index + 1} 时出错: {str(e)}") continue except Exception as e: print(f"处理可点击元素时出错: {str(e)}")
08-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值