现在的位置: 首页 > 综合 > 正文

第一个ios小程序总结

2013年10月17日 ⁄ 综合 ⁄ 共 2935字 ⁄ 字号 评论关闭
An outlet is a  pointer  to an object (a  UILabel in this case).
A  strong  pointer means the  UILabel will stick around until we are done using the  UILabel.
A  weak pointer means the  UILabel will only stick around as long as somebody  else has a  strong  pointer to it.
As soon as no one else has a  strong  pointer to an object that we have a  weak pointer to, that object will go 
away and our pointer to it will be cleared and we won’t be able to talk to it (because it will be gone).
Since this window already has a strong  pointer to this  UILabel,  weak is a good choice here.

@property (weak,nonatomic) IBOutlet UILable *display;

So, whenever our Controller sends messages to the display@property, it will be talking to this  UILabel instance.

IBOutlet  is just a word Xcode throws in here so that it can remember that this is an outlet  @property.
It doesn’t actually mean anything to the compiler.

上面是创建好了显示运算结果和用户输入的Lebal,下面就是创建按钮了

Remember that the term outlet refers to a  @property through which 
we send messages to  something in our View from our Controller (display is an  outlet). 
We use the term action to mean a  method that is going to be sent  from  an object in our View to our Controller when something 
interesting happens in the user-interface.
So our next step is to specify the  action that this  UIButton  is going to send to our Controller when the user touches it. 

一个按钮与我们的代码区进行关联后,创建的是一个action函数

-(IBAction) digitpress: (id) sender{}

IBAction  is exactly the same as  void (i.e. this method does not return any value).  
Xcode uses it instead of void just so it can tell an action method from other methods with a similar form.

You might be surprised that this does not read “ id *”.But that would make no sense because the type “ id” is  already a pointer  
so “ id *” would be a pointer to a pointer.id does not mean “object of any class”, it means “pointer  to an object of any class”.

由于这里可以确定就是按钮发来的信息,要对按钮的动作进行响应,所以可以将上面的函数改为:

-(IBAction) digitpress: (UIButton *) sender{}

接下来就复制这个关联好的按钮,创建其他按钮,这样用这个按钮复制出来的按钮就都是关联到这一个函数了,这点比起MFC好多了,MFC要一个按钮一个函数,麻烦死了。

UIButton 的 currenttitle函数可以返回按钮上面的文本,就可以知道是那个按键被按了,然后这里要做的是,当用户按下按钮时,知道按下确定键之前的都要保存起来,连乘一个字符串,当然还要显示在Lebal上,这个不难实现。但是要加多一个判断,判断用户是第一次按还不不是,是第一次按的话要吧0清掉,不然按下后,数字会接在0后面,那养不符合操作习惯。

在头文件中加入的属性都是public的,要想加入private的属性,就要在.m文件中,对类进行扩展,也就是 声明该类的私有接口,语法就是

@interface classname ();。。。。@end;

注意要加上括号,这样在这之间声明的属性就都是私有的了。

nonatomic means that the setter and getter for this property will  not be thread-safe.
You will always use this keyword unless you really know what you are doing.
We will always use it in this course.
It’s not really a problem because even though we will do lots of multi-threaded 
programming in iOS, virtually all methods  in UIKit must be performed on the
main thread  of execution in your application
(it is non-UI activity that we will put in other threads).

要注意 所有属性声明的时候都是nil值,这个在使用的时候要多加注意。

弄完数字按钮之后就要添加运算按钮(+、-、*、/)这个要重新添加一个按钮控件,不可以复制之前的按钮,不然就会关联到全面的函数去了。

做到这里,就需要在来建立 MVC模式中的model layer了,为了等会可以回来再看现在的界面,Xcode提供了和浏览器一样的多标签界面,新建一个Tab,然后在里面添加一个新类。

接下来这个类所做的工作就是对用户所进行的运算操作进行响应的运算,其中用NSMultableArray模拟栈,要加两个函数,一个是入栈,一个是出栈,然后在进行运算。

写完这些之后,就要回去之前的文件中,import新建的类的头文件,加上一个这个类的属性,使得可以调用这个类中的方法,进行运算。

在调用一些实例方法的时候,需要用到self指针,但是有时候在使用某些属性或者方法的时候会忘记要加上self,这时候编译器是不会报错的,但是程序显然就是不对的,解决这个的方法就是,在synthesize的时候,顺便给该属性附上一个新的名字,这样在使用该属性并且忘记加上self的时候,编译器就会报错了,这也是一个编程的习惯问题。

差不多也就是这样,有些细节就是说了,实在太多。

抱歉!评论已关闭.