全局功能

发布于 2024-10-09 09:00:58 字数 788 浏览 1 评论 0原文

我在我拥有的每个 uitableviewcontroller 中都这样做。

self.navigationController.navigationBar.tintColor= [UIColor blueColor];
UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageName"]];
self.navigationItem.titleView = headerView;

有没有办法创建一个自定义类来为我执行此操作?就像这样

[StyleDefaults setNavStyles];

@implementation StyleDefaults

+(void) setNavStyle {

    self.navigationController.navigationBar.tintColor= [UIColor blueColor];
    UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kheader]];
    self.navigationItem.titleView = headerView;

}

@end

我收到此错误:请求成员“navigationBar”,而不是结构或联合

我知道为什么会出现此错误,但是有办法解决此问题吗?

感谢您抽出时间。

I do this in every uitableviewcontroller I have.

self.navigationController.navigationBar.tintColor= [UIColor blueColor];
UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageName"]];
self.navigationItem.titleView = headerView;

Is there a way I can create a custom class that does this for me? Like such

[StyleDefaults setNavStyles];

where

@implementation StyleDefaults

+(void) setNavStyle {

    self.navigationController.navigationBar.tintColor= [UIColor blueColor];
    UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kheader]];
    self.navigationItem.titleView = headerView;

}

@end

I get this error: request for member 'navigationBar' in something not a structure or union

I know why I get this, but is there a way around this issue?

Thank you for your time.

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

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

发布评论

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

评论(2

很酷又爱笑 2024-10-16 09:00:58

当您将该代码移至另一个类时,self 不再是它所在的 UITableViewController。您可以将对 UITableViewController 的引用传递到该方法中,以便它可以访问这些导航属性。

添加一个 UIViewController 类型的参数(UITableViewControllers 是这些参数的子类)并将 self 替换为该参数:

+(void) setNavStyle:(UIViewController *)viewController {    
    viewController.navigationController.navigationBar.tintColor= [UIColor blueColor];
    UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kheader]];
    viewController.navigationItem.titleView = headerView;
}

然后从 UITableViewControllers 中调用该方法,如下所示:

[StyleDefaults setNavStyle:self];

When you move that code to another class, self is no longer the UITableViewController that it was in. You can pass a reference to the UITableViewController into the method so it can access those navigation properties.

Add a parameter of type UIViewController (UITableViewControllers are subclasses of those) and replace self with the parameter:

+(void) setNavStyle:(UIViewController *)viewController {    
    viewController.navigationController.navigationBar.tintColor= [UIColor blueColor];
    UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kheader]];
    viewController.navigationItem.titleView = headerView;
}

Then call the method from the UITableViewControllers like this:

[StyleDefaults setNavStyle:self];
叹梦 2024-10-16 09:00:58

我建议使用类别来完成此任务。 查看 MacDeveloper 上的此提示
您的界面将如下所示:

@interface UIViewController (myDefaultStyle)
-(void) setupMyStyle;
@end

实现可能如下所示:

@implementation UIViewController (myDefaultStyle)    
  
 -(void) setupMyStyle {

   self.navigationController.navigationBar.tintColor= [UIColor blueColor];
   UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kheader]];
   self.navigationItem.titleView = headerView;

 }

现在,您在实际视图控制器中需要做的所有事情就是导入类别并在有意义的地方调用 [self setupMyStyle]
虽然它与将 self 的引用传递到静态函数具有相同的目的,但它的代码更清晰,甚至可能更快。

I would suggest using a category for this task. See this tip on MacDeveloper
Your interface would look like this:

@interface UIViewController (myDefaultStyle)
-(void) setupMyStyle;
@end

and the implementation could look like this:

@implementation UIViewController (myDefaultStyle)    
  
 -(void) setupMyStyle {

   self.navigationController.navigationBar.tintColor= [UIColor blueColor];
   UIImageView *headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kheader]];
   self.navigationItem.titleView = headerView;

 }

Now everything you'd need to do in your actual view controller is import the category and call [self setupMyStyle] where it makes sense.
While it servers the same purpose as passing a reference to self into a static function,it's clearer code and maybe even a notch faster.

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