为 Objective C 创建通用 UIToolbar 帮助程序文件和方法

发布于 2024-12-07 04:41:45 字数 2415 浏览 2 评论 0原文

我用 UIToolbar 构建了一个视图,效果很好。

该工具栏将出现在应用程序中,现在我正在将代码复制/粘贴到许多不同的文件中。

我不想重复自己,并且希望创建一个帮助程序文件,其中将包括工具栏设置以及链接到我需要的每个文件中的工具栏的方法。

我尝试将以下代码放入 .h .m 文件并从 UIView 继承,但是存在一个问题,因为存在对 self.navigationItem 的引用

有没有一种方法可以让我创建一个通用的 Objective C 文件,该文件将具有我想使用的所有代码和方法?

谢谢。

- (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] initWithImage: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");
    }

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] initWithImage: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");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窗影残 2024-12-14 04:41:45

navigationItemUIViewController 的属性,而不是 UIView。如果您有这样的常见功能,我将从 UIViewController 继承,将您的自定义逻辑添加到 viewDidLoad (或任何合适的地方),然后从中继承您的视图控制器班级。只需确保您从子类的 viewDidLoad 实现中调用 [super viewDidLoad] 即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文