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
优化这段代码import tkinter as tk class TomatoClock: def init(self, work_time=25, rest_time=5, long_rest_time=15): self.work_time = work_time * 60 self.rest_time = rest_time * 60 self.long_rest_time = long_rest_time * 60 self.count = 0 self.is_working = False self.window = tk.Tk() self.window.title("番茄钟") self.window.geometry("300x200") self.window.config(background='white') self.window.option_add("*Font", ("Arial", 20)) self.label = tk.Label(self.window, text="番茄钟", background='white') self.label.pack(pady=10) self.time_label = tk.Label(self.window, text="", background='white') self.time_label.pack(pady=20) self.start_button = tk.Button(self.window, text="开始", command=self.start_timer, background='white') self.start_button.pack(pady=10) def start_timer(self): self.is_working = not self.is_working if self.is_working: self.count += 1 if self.count % 8 == 0: self.count_down(self.long_rest_time) self.label.config(text="休息时间", foreground='white', background='lightblue') elif self.count % 2 == 0: self.count_down(self.rest_time) self.label.config(text="休息时间", foreground='white', background='lightgreen') else: self.count_down(self.work_time) self.label.config(text="工作时间", foreground='white', background='pink') else: self.label.config(text="番茄钟", foreground='black', background='white') def count_down(self, seconds): if seconds == self.work_time: self.window.config(background='pink') else: self.window.config(background='lightgreen' if seconds == self.rest_time else 'lightblue') if seconds == self.long_rest_time: self.count = 0 minute = seconds // 60 second = seconds % 60 self.time_label.config(text="{:02d}:{:02d}".format(minute, second)) if seconds > 0: self.window.after(1000, self.count_down, seconds - 1) else: self.start_timer() def run(self): self.window.mainloop() if name == 'main': clock = TomatoClock() clock.run()
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值