Objective:
Recently has been in the maintenance of the old project, encountered the problem is also strange, need to repair, need to optimize, need special treatment, feel always so silent. Perhaps this time also should sigh a sentence: The road long its repair far XI, I will go up and down and quest it.
This article is just about the old projects encountered in the different kinds of code to write, and encountered some problems when dealing with the method.
1, about the button event repeated click Problem
we have probably experienced this in development, and one of my buttons has been repeatedly clicked quickly, (we developers should be more aware of what this means). Let's take a closer look at (scheduling a particular environment for such an event): For example, when the network is poor, for example, when the button's execution is time-consuming. There are a number of problems: sometimes there is a problem with the interface (when the button executes an event to adjust the interface frame), sometimes there is a lag, and even a crash occurs. For this kind of problem, we have to consider in advance, do a good deal of prevention : That is, click on the button event: add prevent repeated click function.
The idea is to prevent the button from repeating the click
The principle is: each time we click on the button, the first execution of the Cancel button click on the execution of the event, and then to execute a deferred execution method (the method executes the button execution of the event).
Compare the recommended workaround code:
-(void ) btnclicked: (id ) sender{ // here do the buttons to do the things you want to do. "-(void ) buttonclicked: (id ) sender{ // The task before the time of execution is canceled first. class ] Cancelpreviousperformrequestswithtarget:self selector: @selector (btnclicked:) :sender]; [Self performselector: @selector (btnclicked:) withobject:sender afterdelay: ";}
There is also a way that can be achieved: a concrete look at the iOS prevents users from repeatedly tapping the button (buttons) issue "
2. When many interfaces share an interface: Use enumerations to make type judgments
There are many such phenomena in the old project: Many interfaces re-use an interface, which naturally requires a different interface to jump to a reusable interface to make judgments. And the Strange place is: the basis of judgment is self.title. Then there will be a phenomenon: after the jump interface there will be a large paragraph if to determine whether the string is equal to self.title.
If we make some changes: Use the enumeration to make the interface type judgment, use the switch case statement to judge the execution code. This will be more graceful, concise.
3, about the old project in the case of iOS10 above, the navigation bar button does not display the problem
If your viewcontroller all inherit from the base class, then add such a piece of code (which is the easiest way I've found it now) to be in a basic class:
-(void) viewwillappear: (BOOL) animated{ [Super viewwillappear:animated]; [Self.navigationcontroller Setnavigationbarhidden:yes Animated:no]; -(void) viewwilldisappear: (BOOL) animated{ [Super viewwilldisappear:animated]; [Self.navigationcontroller Setnavigationbarhidden:yes Animated:no]; [Self.navigationcontroller Setnavigationbarhidden:no animated:no];}
4, the event method to separate each interface open
This is the case in the old project: when the command clicks on a button to execute an event method or gesture event method, it will somehow jump to another interface.
It is best to differentiate the clicked event naming for different interface buttons when developing, for example: interface name +buttonclicked
5, in the development if the existenceThe image is empty, or the string that must be displayed is empty, remember to make a judgment in the code, the picture can set the default picture directly, the string can also set the default string
For example:
UIImage *image = [UIImage imagenamed:@ "image"]; *defaultimage = [UIImage imagenamed:@ "defaultimage"]; *imageview = [[Uiimageview alloc]initwithimage:imagename imagename:defaultimage];
Project summary Two: the optimization of some functions in the project