[Use of iOS category]---navigation bar leftbarbuttonitem icon Modification

Source: Internet
Author: User
Tags uikit

Categories (category) modify the navigation bar of a specific Viewcontroller item icon

In the project, you encounter such a problem:
When a new VIewController1 wants to push to the original other VIewController2
The original VIewController2 navigation bar left Leftbarbuttonitem icon, became the system comes with the style,
Moreover, because the project mix uses the code and the storyboard development, has added the unification to revise Leftbarbuttonitem the difficulty!

In general, we will redefine a uibarbuttomitem in VIewController2 and insert the following code for local modification
The code is as follows

-(void) changeleftitem{Uiimageview*imagview = [[UiimageviewAlloc] Initwithframe:cgrectmake (0,0, +, +)]; Imagview. Image= [UIImageimagenamed:@"Jiantou"];Uibarbuttonitem*left = [[UibarbuttonitemAlloc] initwithimage:[[UIImageimagenamed:@"Jiantou"] imagewithrenderingmode:uiimagerenderingmodealwaysoriginal] Style:uibarbuttonitemstyledone target: SelfAction@selector(back)]; Self. Navigationitem. Leftbarbuttonitem= left;} -(void) back{[ Self. NavigationcontrollerPopviewcontrolleranimated:YES];}

So the code changes well:

But here's the problem.

If I just modify a section, then the amount of code is acceptable,
And all I want is the next page that VIewController2 push to.
Or VIewController1 push to the VIewController3 page, the arrow icon also to keep, I have to write many times?

I don't want to write a Uiviewcontroller subclass to inherit as the parent class of these classes, what should I do??

This time
Remember that categories (category categories) can extend the methods and functions of a class,

(category) is a way to add a new method to an existing class (including a class cluster: NSString, even a class that the source cannot obtain) without inheriting the subclass from the existing class. The new method added by the category can be inherited by the quilt class. )

Well, then write a category for Uiviewcontroller.

//.h#import <UIKit/UIKit.h>  @interface uiviewcontroller (changeleftitem)-(void) Changeleftitem;@end//.M-(void) changeleftitem{Uiimageview*imagview = [[UiimageviewAlloc] Initwithframe:cgrectmake (0,0, +, +)]; Imagview. Image= [UIImageimagenamed:@"Jiantou"];Uibarbuttonitem*left = [[UibarbuttonitemAlloc] initwithimage:[[UIImageimagenamed:@"Jiantou"] imagewithrenderingmode:uiimagerenderingmodealwaysoriginal] Style:uibarbuttonitemstyledone target: SelfAction@selector(back)]; Self. Navigationitem. Leftbarbuttonitem= left;} -(void) back{[ Self. NavigationcontrollerPopviewcontrolleranimated:YES];}

Then, go to the controller, add the header file name, (or go to the PCH file to add)
Then go to the time category. h inside the method bar, the effect is out!!

* * This is just half the amount of code, if I want to change all the style of the existing arrows? StoryBoard There are so many controllers, I want to realize

    [super viewDidLoad];    [self changeleftItem];

This method??

I have not thought of this question for the time being, write it down first.

Well, I've dealt with the problem just a rough

1. Place the classification header file in the project's PCH file

Then, because it is a classification written by Uiviewcontroller,
So we just go to the global search

[Super Viewdidload];

Then replace it with the following:

    [super viewDidLoad];    [self changeleftItem];

We see a total of nearly 74 replacements, if you want to add them manually, it is estimated that you want to faint

* * Of course, this simple and crude method, also need to note that our project, there are three Rootviewcontroller, and these three places, can not add arrows, so we just need to find these three places, delete the call classification method can be,
At the moment, it's a quicker approach.

PS: Personal feeling it is better to use the method of classification and substitution than simply to write a parent class and then to let many subclasses inherit.

In the already written viewcontroller up to modify their inheritance, the workload is relatively large ~

--end
**

[Use of iOS category]---navigation bar leftbarbuttonitem icon Modification

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.