我正在编写一个 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