全局功能
我在我拥有的每个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您将该代码移至另一个类时,
self
不再是它所在的 UITableViewController。您可以将对 UITableViewController 的引用传递到该方法中,以便它可以访问这些导航属性。添加一个 UIViewController 类型的参数(UITableViewControllers 是这些参数的子类)并将
self
替换为该参数:然后从 UITableViewControllers 中调用该方法,如下所示:
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:Then call the method from the UITableViewControllers like this:
我建议使用类别来完成此任务。 查看 MacDeveloper 上的此提示
您的界面将如下所示:
实现可能如下所示:
现在,您在实际视图控制器中需要做的所有事情就是导入类别并在有意义的地方调用
[self setupMyStyle]
。虽然它与将 self 的引用传递到静态函数具有相同的目的,但它的代码更清晰,甚至可能更快。
I would suggest using a category for this task. See this tip on MacDeveloper
Your interface would look like this:
and the implementation could look like this:
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.