开发者

Creating a common UIToolbar helper file and methods for Objective C

I have build a view with a UIToolbar which is working great.

This toolbar will be appearing right across the app, and right now I am copy/pasting the code into lots of different files.

I do not want to repeat myself, and am looking to create a helper file that will include the toolbar setup and the methods linked to the toolbar in every file I need.

I've tried putting the following code into a .h .m file and inheriting from UIView, but there is a problem because there is a reference to self.navigiationItem

Is there a way that I can create a common Objective C file that will have all the code and methods I want to use?

Thanks.

- (void)viewDidLoad
   // ... 

    // appears in viewDidLoad
     // ---- TOOLBAR -----------//

        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100.0, 44.01f)];
        //[toolbar setBackgroundColor:[UIColor blackColor]];
        //[toolbar setTintColor:[UIColor redColor]];
        //[toolbar.layer setBorderColor:[[UIColor redColor] CGColor]];

        // Bar buttons

        UIBarButtonItem *barReloadBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(btnReload:)];

        [barReloadBtn setStyle:UIBarButtonItemStyleBordered];

        // Profile bar button
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"111-user" ofType:@"png"]];

        UIBarButtonItem *barProfileBtn = [[UIBarButtonItem alloc] init开发者_JAVA技巧WithImage:image style:UIBarButtonItemStyleBordered target:self action:@selector(btnProfile:)];


        // Button array

        NSMutableArray *buttons = [[NSMutableArray alloc] init]; 

        [buttons addObject:barProfileBtn];
        [buttons addObject:barReloadBtn];


        [toolbar setItems:buttons];

        // Set nav items

        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];

        // memory cleanup
        [image release];
        [buttons release];
        [barReloadBtn release];
        [barProfileBtn release];    
        [toolbar release];


        // ---- /TOOLBAR -----------//
    }



    #pragma mark - IBActions

    -(IBAction) btnProfile:(id)sender
    {
        UserProfileVC *userProfileVC = [[UserProfileVC alloc] initWithNibName:@"UserProfileVC" bundle:[NSBundle mainBundle]];

        UINavigationController *tmpNavCon = [[UINavigationController alloc] initWithRootViewController:userProfileVC];

        [self.navigationController presentModalViewController:tmpNavCon animated:YES];

        [tmpNavCon release];
        [userProfileVC release];
    }

    -(IBAction) btnReload:(id)sender
    {
        NSLog(@"Not done yet");
    }


navigationItem is a property of UIViewController, not UIView. If you've got common functionality like this, I would inherit from UIViewController, add your custom logic to viewDidLoad (or wherever is appropriate) and then inherit your view controllers from that class. Just make sure you call [super viewDidLoad] from your subclasses' implementations of viewDidLoad.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜