Xcode学习之路:Interface Builder使用技巧与UIAlertView笔记

发布时间:2026/9/30 6:20:26
Xcode学习之路:Interface Builder使用技巧与UIAlertView笔记 最近在看电驴上down下来的ipad开发视频教程。为加深记忆特在此做笔记。前两集视频主要讲的是UIAlertView相当于windows里面的MessageBox在interface builder里面添加了控件之后要想使得控件响应事件必须做如下处理1在XXXViewController.h里面添加事件响应函数声明如-(IBAction)btnOnclick:(id)sender;2在相应的.m文件中实现这个函数3回到interface builder中鼠标右键从需要使用这个事件相应函数的控件拖拽到File’s Owner上并在弹出的选项中选中步骤1中的函数名使之关联起来。注意这个视频里面提到了一个特别的控件就是UIAlertView因为这个控件没有在interface builder中提供出来编辑。所以如果这个要使这个控件的事件得到相应需要在XXXViewController.h文件里面声明类的时候使用协议(类似C里面的抽象基类的多重继承)。样子如下#import UIKit/UIKit.h interface AlertViewTestViewController : UIViewController UIAlertViewDelegate{ } end然后在实现文件里面实现对应的事件处理函数。对UIAlertView来说。要使用的协议是UIAlertViewDelegate要实现的事件响应函数是 - (void)alertView:(UIAlertView* )alertView clickedButtonAtIndex:(NSInteger)buttonIndex。实际上使用了协议之后可以根据协议名字在帮助里面很快的找到这个协议需要实现哪些函数。控件还有另外一个比较重要的东西就是数据绑定。在xcode里面要实现这么一个东西步骤基本和上面的类似1在XXXViewController.h文件里面声明之下面声明了一个文本数据interface OutletAndActionViewController : UIViewController { IBOutlet UITextField *txtName; } property(nonatomic, retain) UITextField *txtName; end2实现文件里面给出set和get函数。如果使用了属性你懂的。。。synthesize txtName;3回到interface builder中鼠标右键从File’s Owner中拖拽到需要绑定这个数据的控件上并在弹出的选项中选中使之关联起来。拖拽方向和上面相反。写到这里。我在想IBAction和IBOutlet到底是个啥在头文件中找到了他们的定义#ifndef IBOutlet #define IBOutlet #endif #ifndef IBAction #define IBAction void #endif几乎啥都没干。最后在cocoachina上找到了答案。原帖http://www.cocoachina.com/bbs/read.php?tid-18829.html内容如下These two keywords do absolutely nothing as far as the compiler is concerned. IBOutlet gets entirely removed from the code before the compiler ever sees it. IBAction resolves to a void return type, which just means that action methods do not return a value. So, what’s going on here?The answer is simple, really: IBOutlet and IBAction are not used by the compiler. They are used by Interface Builder. Interface Builder uses these keywords to parse out the outlets and actions available to it. Interface Builder can only see methods that are prefaced with IBAction and can only see variables or properties that are prefaced with IBOutlet. Also, the presence of these keywords tells other programmers, looking at your code in the future, that the variables and methods in question aren’t dealt with entirely in code. They’ll need to delve into the relevant nib file to see how things are hooked up and used.敢情这两关键字完全是给interface builder看的。对于追求更高效开发体验的团队快蝎kxapp作为新一代 iOS 开发 IDE无需安装 Xcode 即可完成从编码到真机调试的全流程内置编译工具套装让开发者更专注于逻辑实现。小结Xcode学习之路Interface Builder使用技巧 的内容介绍完了希望本文对你有所帮助

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询