[问题]
已有OC代码:
MHTabBarController.h
@protocol MHTabBarControllerDelegate; /* * A custom tab bar container view controller. It works just like a regular * UITabBarController, except the tabs are at the top and look different. */ @interface MHTabBarController : UIViewController …... @end /* * The delegate protocol for MHTabBarController. */ @protocol MHTabBarControllerDelegate <NSObject> @optional - (BOOL)mh_tabBarController:(MHTabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController atIndex:(NSUInteger)index; - (void)mh_tabBarController:(MHTabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController atIndex:(NSUInteger)index;@end
对应的MHTabBarController.m:
- (void)setSelectedIndex:(NSUInteger)newSelectedIndex animated:(BOOL)animated { NSAssert(newSelectedIndex < [self.viewControllers count], @"View controller index out of bounds"); if ([self.delegate respondsToSelector:@selector(mh_tabBarController:shouldSelectViewController:atIndex:)]){...} - (BOOL)mh_tabBarController:(MHTabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController atIndex:(NSUInteger)index { NSLog(@"mh_tabBarController %@ shouldSelectViewController %@ at index %u", tabBarController, viewController, index); // Uncomment this to prevent "Tab 3" from being selected. //return (index != 2); return YES; } - (void)mh_tabBarController:(MHTabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController atIndex:(NSUInteger)index { NSLog(@"mh_tabBarController %@ didSelectViewController %@ at index %u", tabBarController, viewController, index);}
现在需要去:
把OC中的对应的delegate和protocol,尤其是:
shouldSelectViewController
didSelectViewController
实现为对应的Swift版本,并且对应的setSelectedIndex中,要去调用对应的respondsToSelector的shouldSelectViewController
[解决过程]
1.搜:
oc delegate convert to swift
swift delegate protocol
参考:
Converting an Objective-C app to Swift: Working with web data – Tutorial – Binpress
World-Changing Apps From Hawaii
The Swift Programming Language (Swift 2): Protocols
Learn Swift from Objective-C : Protocols and Delegation
Swift from Scratch: Delegation and Properties – Tuts+ Code Tutorial
How Delegation Works – A Swift Developer’s Guide – Andrew Bancroft
Delegates in swift? – Stack Overflow
NSCurious | Create protocol and implement delegation in Swift
2.那就继续去学习:
[整理]Swift的代理delegate,协议protocol
3.其中又需要去:
[已解决]Swift中的delegate中的函数如何定义和实现
4.然后就可以继续去折腾代码了。
然后需要看懂,OC中的respondsToSelector
貌似Swift中的delegate不需要写了?
还是去研究看看: