求问如何在viewController里使用两个以上的 uialertview按钮颜色

2712人阅读
iPhone开发指南(27)
转载请注明出处,原文网址:
作者:张燕广
实现的功能:演示Picker View的使用方法
关键词:Picker View
1、创建一个Single View Application工程,命名为:PickersDemo,如下图
2、修改ViewController.xib,添加一个Picker View和一个Button控件,如下
连接输出口、操作,如下
3、视图控制器ViewController,需要实现协议UIPickerViewDataSource、UIPickerViewDelegate中的必须实现的方法,
在工程目录依次展开Frameworks-&UIKit.framework-&Headers,然后打开UIPickerView.h,在文件的尾部可以看到协议UIPickerViewDataSource、UIPickerViewDelegate的定义,如下
4、修改ViewController.h:
ViewController.h
PickersDemo
Created by Zhang Yanguang on 12-10-25.
Copyright (c) 2012年 MyCompanyName. All rights reserved.
#import &UIKit/UIKit.h&
#define kDeviceCategory 0
#define kDeviceName 1
@interface ViewController : UIViewController
@property(strong,nonatomic)IBOutlet UIPickerView *
@property(nonatomic,retain)NSDictionary *appleD
@property(nonatomic,retain)NSArray *deviceC
@property(nonatomic,retain)NSArray *deviceN
-(IBAction)buttonPressed:(id)
5、修改ViewController.m:
ViewController.m
PickersDemo
Created by Zhang Yanguang on 12-10-25.
Copyright (c) 2012年 MyCompanyName. All rights reserved.
#import &ViewController.h&
@interface ViewController ()
@implementation ViewController
@synthesize appleD
@synthesize deviceC
@synthesize deviceN
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array1 = [NSArray arrayWithObjects:@&iPhone&,@&iPad&,@&iPod&,nil];
NSArray *array2 = [NSArray arrayWithObjects:@&Mac&,@&iMac&,@&Mac Mini&,@&Mac Pro&,nil];
NSDictionary
*dictionary= [NSDictionary dictionaryWithObjectsAndKeys:array1,@&Mobile&,array2,@&Computers&,nil];//注意用nil结束
appleDevices = [[NSDictionary alloc]initWithDictionary:dictionary copyItems:YES];
NSArray *components = [self.appleDevices allKeys];
NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];
self.deviceCategory =
NSString *selectedCategory = [self.deviceCategory objectAtIndex:0];
self.deviceName = [self.appleDevices objectForKey:selectedCategory];
- (void)viewDidUnload
[super viewDidUnload];
appleDevices =
deviceCategory =
deviceName =
// Release any retained subviews of the main view.
-(void)dealloc{
[picker release];
[appleDevices release];
[deviceCategory release];
[deviceName release];
-(IBAction)buttonPressed:(id)sender{
NSString *selectedCategory = [self.deviceCategory objectAtIndex:[self.picker selectedRowInComponent:kDeviceCategory]];
NSString *selectedDevice = [self.deviceName objectAtIndex:[self.picker selectedRowInComponent:kDeviceName]];
//NSLog(@&%@-%@&,selectedCategory,selectedDevice);
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@&结果& message:[NSString stringWithFormat:@&%@:%@&,selectedCategory,selectedDevice]
delegate:self cancelButtonTitle:@&OK& otherButtonTitles:nil];
[alertView show];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
#pragma mark Picker View
DataSource methods
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if(component == kDeviceCategory){
return [self.deviceCategory count];
return [self.deviceName count];
#pragma mark Picker View
Delegate methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if(component == kDeviceCategory){
return [self.deviceCategory objectAtIndex:row];
return [self.deviceName objectAtIndex:row];
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if(component == kDeviceCategory){
NSString *selectedCategory = [self.deviceCategory objectAtIndex:row];
NSArray *array = [self.appleDevices objectForKey:selectedCategory];
self.deviceName =
[self.picker selectRow:0 inComponent:kDeviceName animated:YES];
[self.picker reloadComponent:kDeviceName];
6、编译、运行,效果如下:
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:643284次
积分:8381
积分:8381
排名:第1325名
原创:175篇
转载:25篇
评论:201条
自2011年起,从事Android&IOS App研发
现从事手游研发
目前关注移动App&Game研发、运营推广、盈利
QQ:欢迎加Q探讨交流
PS:最近1年很少登录CSDN,所以发私信的朋友未能及时回复,抱歉!
文章:28篇
阅读:135537
阅读:23582
(4)(1)(2)(2)(5)(5)(13)(12)(10)(4)(17)(16)(2)(1)(1)(5)(13)(2)(3)(1)(2)(3)(3)(1)(1)(3)(6)(7)(11)(21)(6)(1)(4)(1)(3)(5)(1)(1)(1)UIAlertShimPresentingViewController shouldAutorotate] is ret
今天做开发的时候遇到一个很大的问题,就是不需要屏幕的旋转,所以我在UINavigationController中设置了屏幕的方向,但是在UIAlertView提示的地方出现这样的错误
今天做开发的时候遇到一个很大的问题,,就是不需要屏幕的旋转,所以我在UINavigationController中设置了屏幕的方向,但是在UIAlertView提示的地方出现这样的错误:
&Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [_UIAlertShimPresentingViewController shouldAutorotate]
is returning YES'
很是郁闷,在网上找到具体解决方法:
通过查阅官方文档,发现supportedInterfaceOrientations方法的返回值是UIInterfaceOrientationMask类型的,所以应该用UIInterfaceOrientationMaskPortrait。UIInterfaceOrientationMask类型从iOS6就有了,只不过到iOS8.3才会崩溃。
至于preferredInterfaceOrientationForPresentation方法,返回值还是老的UIInterfaceOrientation类型。
你最喜欢的如果按下A中的按钮2会弹出一个UIAlertView C,我希望能在按下C的按钮3后,能直接跳转到B。请问用什么函数实现?多谢解答!
我把问题中的UIViewController定义成PageA, PageB,即上面的A, B按钮定义为Button B(完成直接push 到PageB),Button alert(用于弹出UIAlertView)storyboard截图如下因为我没有专门写类来标识PageB, 所以需要在storyboard中用storyboardID来标识一下(把PageB标识为PageB)Button B是直接用Interface Builder直接创建的segue(大家都懂的)Button B是直接用Interface Builder直接创建的segue(大家都懂的)alert 按钮需要用代码来action一下(绑定控件到代码大家都懂的)直接上图看代码主要消息是主要消息是- (IBAction)alert:(UIButton *)sender;//用来触发alertView显示在屏幕上- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;//用来处理delegate消息点击Button Alert, 激活- (IBAction)alert:(UIButton *)sender代码点击Button Alert, 激活- (IBAction)alert:(UIButton *)sender代码点击OK触发delegate的消息进入PageB
已有帐号?
社交帐号登录
无法登录?
社交帐号登录23948人阅读
iOS的SDK中提供很多原生ViewController,大大提高了我们的开发效率,下面是我的一些经验。
按结构可以对iOS的所有ViewController分成两类:
1、主要用于展示内容的ViewController,这种ViewController主要用于为用户展示内容,并与用户交互,如UITableViewController,UIViewController。
2、用于控制和显示其他ViewController的ViewController。这种ViewController一般都是一个ViewController的容器。如UINavigationController,UITabbarController。它们都有一个属性:viewControllers。其中UINavigationController表示一种Stack式结构,push一个ViewController或pop一次,因此后一个ViewController一般会依赖前一个ViewController。而UITabbarController表示一个Array结构,各个ViewController是并列的。
第一种ViewController会经常被继承,用来显示不同的数据给用户。而第二种很少被继承,除非你真的需要自定义它。
注:细心的同学应该能发现,在Xcode中新建一个ViewController时,只可以选择继承自UIViewController和UITableViewController,而它们都是第一种。
这里指的View是指Controller的View。它作为Controler的属性,生命周期在Controller的生命周期内。就是说你的Controller不能在view释放前就释放了。
图2 ViewController生命周期
当你alloc并init了一个ViewController时,这个ViewController应该是还没有创建view的。ViewController的view是使用了lazyInit方式创建,就是说你调用的view属性的getter:[self view]。在getter里会先判断view是否创建,如果没有创建,那么会调用loadView来创建view。loadView完成时会继续调用viewDidLoad。loadView和viewDidLoad的一个区别就是:loadView时还没有view。而viewDidLoad时view以及创建好了。
当view被添加其他view中之前时,会调用viewWillAppear,而之后会调用viewDidAppear。
当view从其他view中移出之前时,会调用viewWillDisAppear,而之后会调用viewDidDisappear。
当view不在使用,而且是disappeared,受到内存警告时,那么viewController会将view释放并将其指向nil。
ViewController生命周期中有那么多函数,一个重要问题就是什么代码该写在什么地方。
1、init里不要出现创建view的代码。良好的设计,在init里应该只有相关数据的初始化,而且这些数据都是比较关键的数据。init里不要掉self.view,否则会导致viewcontroller创建view。(因为view是lazyinit的)。
2、loadView中只初始化view,一般用于创建比较关键的view如tableViewController的tabView,UINavigationController的navgationBar,不可掉用view的getter(在掉super loadView前),最好也不要初始化一些非关键的view。如果你是从nib文件中创建的viewController在这里一定要首先调用super的loadView方法,但建议不要重载这个方法。
3、viewDidLoad 这时候view已经有了,最适合创建一些附加的view和控件了。有一点需要注意的是,viewDidLoad会调用多次(viewcontroller可能多次载入view,参见图2)。
4、viewWillAppear 这个一般在view被添加到superview之前,切换动画之前调用。在这里可以进行一些显示前的处理。比如键盘弹出,一些特殊的过程动画(比如状态条和navigationbar颜色)。
5、viewDidAppear 一般用于显示后,在切换动画后,如果有需要的操作,可以在这里加入相关代码。
6、viewDidUnload 这时候viewController的view已经是nil了。由于这一般发生在内存警告时,所以在这里你应该将那些不在显示的view释放了。比如你在viewcontroller的view上加了一个label,而且这个label是viewcontroller的属性,那么你要把这个属性设置成nil,以免占用不必要的内存,而这个label在viewDidLoad时会重新创建。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:297502次
积分:2959
积分:2959
排名:第7281名
原创:30篇
转载:58篇
评论:20条
(1)(1)(1)(4)(3)(1)(6)(5)(6)(1)(2)(10)(7)(4)(6)(18)(12)ios segmented controls可不可以在两个页面之间跳转_百度知道
ios segmented controls可不可以在两个页面之间跳转
  这就是segmentedController。  1在RootViewController.m的viewDidLoad添加如下代码:  NSArray&*array&=&[NSArray&arrayWithObjects:@&鸡翅&,@&排骨&,&nil];  UISegmentedControl&*segmentedController&=&[[UISegmentedControl&alloc]&initWithItems:array];  segmentedController.segmentedControlStyle&=&UISegmentedControlSegmentC    [segmentedController&addTarget:self&action:@selector(segmentAction:)&forControlEvents:UIControlEventValueChanged];  self.navigationItem.titleView&=&segmentedC  2[segmentedController addTarget:selfaction:的实现  -(void)segmentAction:(id)sender  {  switch&([sender&selectedSegmentIndex])&{  case&0:  {  UIAlertView&*alter&=&[[UIAlertView&alloc]&initWithTitle:@&提示&&message:@&你点击了鸡翅&&delegate:self&&cancelButtonTitle:@&确定&&otherButtonTitles:nil,&nil];  [alter&show];    }    case&1:  {  UIAlertView&*alter&=&[[UIAlertView&alloc]&initWithTitle:@&提示&&message:@&你点击了排骨&&delegate:self&&cancelButtonTitle:@&确定&&otherButtonTitles:nil,&nil];  [alter&show];  }      default:    }  }  -(void)segmentAction:(id)sender{ & &switch ([sender selectedSegmentIndex]) { & & & &case 0: & & & &{ & & & & & &UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@&提示& message:@&你点击了鸡翅& delegate:self &cancelButtonTitle:@&确定& otherButtonTitles:nil, nil]; & & & & & &[alter show]; & & & &} & & & & & &case 1: & & & &{ & & & & & &UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@&提示& message:@&你点击了排骨& delegate:self &cancelButtonTitle:@&确定& otherButtonTitles:nil, nil]; & & & & & &[alter show]; & & & &} & & & & & & & & & & & &default: & & & & & & & &}}这样就能响应鸡翅和排骨按钮了  
其他类似问题
为您推荐:
ios的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 uialertview输入框 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信