UITabBaruipagecontrollerr管理着几个UINavigationuipagecontrollerr时怎么横屏

IOS开发中创建TabBarController和UINavigationController的一种方法 - 简书
您还可以通过以下方式直接登录
下载简书移动应用
IOS开发中创建TabBarController和UINavigationController的一种方法
在IOS开发中创建TabBarController和UINavigationController是很常见的,如果是手写代码创建而不是用Storyboard创建,由于涉及到较多的ViewController(一般为五个),显得比较麻烦,并且假如选择的方法过于笨拙,则会产生大量的“重复代码”。我在实践的过程中总结出下面这样一种创建方法,来避免大量“重复代码”的出现。在上代码之前,首先假设我要做五个ViewController,他们的类名分别为FirstViewController、SecondViewController、ThirdViewcontroller、FourthViewController、FifthViewController。现在开始上代码:
NSArray *classNameArray = @[@"FirstViewController",@"SencondViewController",@"ThirdViewController",@"FourthViewController",@"FifthViewController"];
NSArray *titleArray = @[@"first",@"sencond",@"third",@"fourth",@"fifth"];
NSMutableArray *navigationControllerArray = [[NSMutableArray alloc] init];
for (int i=0; i&classNameArray. i++) {
UIViewController *viewController = [[NSClassFromString(classNameArray[i]) alloc] init];
//设置title
viewController.title = titleArray[i];
//设置tabbarItem图片
UIImage *normalImage = [UIImage imageNamed:[NSString stringWithFormat:@"btn_%@_正常",titleArray[i]]];
UIImage *selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"btn_%@_点击",titleArray[i]]];
if (IOS7) {
viewController.tabBarItem.image = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
viewController.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[viewController.tabBarItem setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:normalImage];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[array addObject:navigationController];
UITabBarController *tabBatController = [[UITabBarController alloc] init];
tabBatController.viewControllers =
self.window.rootViewController = tabBatC
//设置UITabBarItem属性
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:14.0f]} forState:UIControlStateNormal];
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:20/255.0 green:152/255.0 blue:172/255.0 alpha:1], NSFontAttributeName:[UIFont fontWithName:@"AmericanTypewriter" size:14.0f]} forState:UIControlStateSelected];
tabBarController.viewControllers = navigationControllerA
tabBarController.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"item_selected_background.png"];
tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabbar_background.png"];
以上代码即可完成TabBarController和UINavigationController的创建,其中IOS7为:#define IOS7 [[[UIDevice currentDevice] systemVersion] floatValue] &= 7.0 表示版本的判断
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
作者的其他热门文章:
你可能会感兴趣:评论 - 440【已解决】iOS程序中,设置delegate时候,出现警告:Assigning to ‘id&UINavigationControllerDelegate,UIImagePickerControllerDelegate&’ from incompatible type ‘AddSightingViewController *const __strong’
iOS项目中,给一个controller设置一个delegate时,出现警告:
/Users/crifan/develop/ios_sdk/iphone/BirdWatching/BirdWatching/BirdWatching/AddSightingViewController.m:60:39: Assigning to ‘id&UINavigationControllerDelegate,UIImagePickerControllerDelegate&’ from incompatible type ‘AddSightingViewController *const __strong’
【解决过程】
1.问题很明显是 strong不兼容,然后就去试试,把相应的AddSightingViewController改一下,从
@property (nonatomic) UIImagePickerController *imgPickerC
@property (nonatomic, weak) UIImagePickerController *imgPickerC
然后就消除了此警告了。
注:有时候Xcode不能及时消除警告和错误,需要手动去Product-&Clean一下。
好像Property变量,如果不指定引用类型,则默认是strong引用,此处改为weak,即可解除此警告。
而另外关于weak,strong等更详细的含义,有空再整理。
实际上,刚才此问题根本没有解决。
现在继续折腾。
其解释和我此处不符合,因为我已经在头文件中声明了:
@interface AddSightingViewController : UITableViewController &UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate, CLLocationManagerDelegate, MKMapViewDelegate, UIImagePickerControllerDelegate&
NSArray *birdNameL
CLLocationCoordinate2D location2D;
但是还是有警告。
2.后来找到这里:
其解释很清楚:
主要是UIImagePickerController
有两个protocol:UINavigationControllerDelegate和UIImagePickerControllerDelegate,而此处当前对象self为AddSightingViewController,其只在.h中定义了,符合UIImagePickerControllerDelegate,而没有说也符合UINavigationControllerDelegate,因此才报错的。
其解释,也和我刚才猜想的一致。
只是我刚才在官网:
中没有看到这样:
@property (nonatomic, assign) id &UINavigationControllerDelegate, UIImagePickerControllerDelegate&
的定义罢了,所以以为自己的理解有误呢。
然后又发现,其实就是自己没注意看,因为上述的delegate的定义,本来就不在UIImagePickerControllerDelegate中,而在
中,对应定义为:
The image picker’s delegate object.
@property (nonatomic, assign) id&UINavigationControllerDelegate, UIImagePickerControllerDelegate& delegate
Discussion
The delegate receives notifications when the user picks an image or movie, or exits the picker interface. The delegate also decides when to dismiss the picker interface, so you must provide a delegate to use a picker. If this property is nil, the picker is dismissed immediately if you try to show it.
For information about the methods you can implement for your delegate object, see .
Availability
Available in iOS 2.0 and later.
Related Sample Code
Declared In
UIImagePickerController.h
但是很无语的是,刚开始看官网文档的时候,其解释为:
Providing a Delegate Object
To use an image picker controller, you must provide a delegate that conforms to the
protocol. Starting in iOS 4.1, you can use the delegate to save still-image metadata to the Camera Roll along with the image. See .
很明显,其并没有提及UINavigationControllerDelegate,而只说要符合UIImagePickerControllerDelegate,导致写代码的之后,肯定也只会添加一个delegate:UIImagePickerControllerDelegate,而不会想到UINavigationControllerDelegate。
所以,问题很明确了,就是去添加对应的声明,符合UINavigationControllerDelegate即可。
@interface AddSightingViewController : UITableViewController &UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate, CLLocationManagerDelegate, MKMapViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate&
NSArray *birdNameL
CLLocationCoordinate2D location2D;
结果很悲催的,点击那个图片后,没有出现预料的Album,却在presentViewController的位置出错了:
所以,也要接着处理这个问题。
结果发现是上面的UIImagePickerController变量更改导致的,即再从:
@property (nonatomic, weak) UIImagePickerController *imgPickerC
@property (nonatomic) UIImagePickerController *imgPickerC
就可以正常切换界面,到Album了。
结果,接下来,也就可以正常执行了。
设置delegate时候,出现警告:Assigning to ‘id&UINavigationControllerDelegate,UIImagePickerControllerDelegate&’ from incompatible type ‘AddSightingViewController *const __strong’
解决方法:
去.h中添加:
@interface xxx : xxx &xxx, UIImagePickerControllerDelegate, UINavigationControllerDelegate&
以符合UIImagePickerControllerDelegate和UINavigationControllerDelegate,即可解决问题。
2015年五月
11121314151617
18192021222324
25262728293031
免费的格式化Javascript源码的网站
查询Unicode字符,且还带Oct,Decimal,Hex,HTML Entity
HTML和Javascript都支持,很好用。UINavigationController和UITabBarController合用 - lovefqing的专栏
- 博客频道 - CSDN.NET
2459人阅读
开发环境:Xcode4.5
很多时候我们创建一个基于UITabBarController的application以后还希望能够在每个tab view都可以实现导航控制,即添加一个UINavigationController来实现tabview内部的view之间的切换,这即是本文所要介绍的。
一、创建一个 Tabbed Application.默认创建的是带有两个Tab的工程。
二、在AppDelegate.h里面添加
@property (strong, nonatomic) UINavigationController *NaviView1C
@property (strong, nonatomic) UINavigationController *NaviView2C
三、修改AppDelegate.m文件的didFinishLaunchingWithOptions函数,在这里我们设置相应的UINavigationController.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@&FirstViewController& bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@&SecondViewController& bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = @[viewController1, viewController2];
self.window.rootViewController = self.tabBarC
[self.window makeKeyAndVisible];
return YES;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@&FirstViewController& bundle:nil] autorelease];
viewController1.title = @&View1&;
self.NaviView1Controller = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@&SecondViewController& bundle:nil] autorelease];
viewController2.title = @&View2&;
self.NaviView2Controller = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = @[self.NaviView1Controller, self.NaviView2Controller];
self.window.rootViewController = self.tabBarC
[self.window makeKeyAndVisible];
return YES;
运行一下程序,可以看到每个Tab view都自动多了一个NavigationBar在顶部。
三、添加两个新的View测试NavigationController。
1. 创建Test1ViewController类,以及对应的xib文件。
2.&创建Test2ViewController类,以及对应的xib文件。
3. 如果你的iPhone iOS版本低于6.0,在xib的设置里需要勾掉autolayout选项。
四、在Tab view 1的xib文件里添加一个按钮,并设置相应的IBOutlet, IBAction.如下图:
五、编写代码逻辑。
在FirstViewController.m中。编写函数gotoTest1View
- (IBAction)gotoTest1View:(id)sender {
Test1ViewController *SRDVController = [[Test1ViewController alloc]init];
[self.navigationController pushViewController:SRDVController animated:YES];
[SRDVController release];
同理可以测试,Test2ViewController.不再赘述。
六、代码下载:
若本文和代码有任何错误,欢迎拍砖指正!
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:82697次
积分:1030
积分:1030
排名:千里之外
原创:15篇
评论:26条
/beniao/archive//1931028.html
(2)(1)(6)(1)(2)(1)(1)(2)(1)(2)(2)(1)

我要回帖

更多关于 ui router controller 的文章

 

随机推荐