【发布时间】:2012-11-16 09:22:22
【问题描述】:
我正在编写一个 Rubymotion 应用程序,我想自定义 TabBar。在 NSScreencasts.com 上,他们解释了如何在 Objective-C 中执行此操作,但是如何将以下代码转换为 Ruby?
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self customize];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self customize];
}
return self;
}
- (void)customize {
UIImage *tabbarBg = [UIImage imageNamed:@"tabbar-background.png"];
UIImage *tabBarSelected = [UIImage imageNamed:@"tabbar-background-pressed.png"];
[self setBackgroundImage:tabbarBg];
[self setSelectionIndicatorImage:tabBarSelected];
}
@end
这是我的尝试:
class CustomTabbar < UITabBarController
def init
super
customize
self
end
def customize
tabbarBg = UIImage.imageNamed('tabbar.jpeg')
self.setBackgroundImage = tabbarBg
end
end
但是如果我运行它,我会得到这个错误:
Terminating app due to uncaught exception 'NoMethodError', reason: 'custom_tabbar.rb:5:in `init': undefined method `setBackgroundImage=' for #<CustomTabbar:0x8e31a70> (NoMethodError)
更新
*这是我的 app_delete 文件:*
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
first_controller = FirstController.alloc.init
second_controller = SecondController.alloc.init
tabbar_controller = CustomTabbar.alloc.init
tabbar_controller.viewControllers = [first_controller, second_controller]
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
@window.rootViewController = tabbar_controller
@window.makeKeyAndVisible
true
end
end
【问题讨论】:
-
你试过
self.backgroundImage = tabbarBg或self.setBackgroundImage(tabbarBg)吗? -
我还看到一个问题,您将
UITabBarController子类化(这是一个控制器),但您应该将UITabBar子类化(这是一个 UIView) -
请使用
UITabBar和self.backgroundImage = tabbarBg再试一次,然后发布您收到的错误消息 -
如果我不继承 UITabBarControllera 并设置 self.backgroundImage = tabbarBg 我得到这个错误: undefined method `viewControllers=' for #<0x6901600>0x6901600>