mplementing the Model Object
We will create a new class whose instance represents an object of the calculator, follow the following steps:
1) Click on the “Classes” folder.
2) Choose File > New File.
3) Choose Cocoa Touch Classes > NSObject subclass.
4) Name the class “iCalculator”, make sure that creating the header file option is selected.
5) Insert the following Code into iCalculator.h:
Figure 3: iCalculator.h.
6) Insert the following Code into iCalculator
Figure 4: iCalculator.m part I.
Notes for newbies:
1) The “retain” and “release” methods are used mainly in the memory management of iPhone applications, that’s because there isn’t any garbage collection in the iPhone OS.
Thus, memory management is done manually; you can’t simply call “dealloc” to free the resources and references of a certain object, because this object may contain references to other objects that will not be de-allocated, and thus will cause memory leaks, so we use retain which will increment the value of a certain variable for this instantiated object, and release decrements the value, and when it reaches zero, the referenced object is de-allocated.
For more information about this topic, read more about memory management in Objective C++ 2.0.
2) If ( Self = [ super init ] )
What does this mean? This means that init of the parent class “NSObject” function will be called and if self is not equals to null do the following.
Figure 5: iCalculator.m part II.
Figure 6: iCalculator.m part III.
Implementing the controller object:
1) Click on classes.
2) Choose File > New File
3) Choose iPhone OS> Cocoa Touch Classes > UIViewController subclass.
4) Name the class “iCalcViewController”.
5) Modify the iCalcViewController.h header file as the following:
Figure 7: iCalcViewController.h
6) Modify the iCalcViewController.m file as the following:
Figure 8: iCalcViewController.m part I.
Figure 9: iCalcViewController.m part II
沒有留言:
發佈留言