標籤:des style http java 使用 strong
UIActionSheet:
首先,在.h檔案中添加Protocol,(Protocol相當於Java中的interface)@interface ActionSheetViewController : UIViewController
<UIActionSheetDelegate>
{......}-(IBAction) showActionSheetButtonPressed:(id) sender;-(IBAction) showAlterViewButtonPressed:(id) sender;
在.m檔案中實現showActionSheetButtonPressed 方法。-(IBAction) showActionSheetButtonPressed:(id) sender { UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"Title"
delegate:self
cancelButtonTitle:@"Cancel!"
destructiveButtonTitle:@"OK!"
otherButtonTitles:nil];
[actionSheet showInView:self.view];//參數指顯示UIActionSheet的parent。
[actionSheet release];
}一個IUActionSheet在使用者點擊Button的時調用,但是當使用者選擇了destructiveButton或者cancelButton後,如何處理想對應的事件呢?在UIActionSheetDelegate中,有一個方法,-(void) actionSheet :(UIActionSheet *) actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;我們需要實現這個方法就可以了。 -(void) actionSheet :(UIActionSheet *) actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex{//當使用者按下cancel按鈕if( buttonIndex == [actionSheet cancelButtonIndex]){// DoSomething here.}//當使用者按下OK按鈕if( buttonIndex == [actionSheet destructiveButtonIndex]){// DoSomething here.}} UIAlertView:-(IBAction) showAlterViewButtonPressed:(id) sender{UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Alert Title"
message:@"Alter Message Content"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
[alertView release];
}
1. 最簡單的用法
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"這是一個簡單的警告框!"
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alert show];
[alert release];
2. 為UIAlertView添加多個按鈕
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"請選擇一個按鈕:"
delegate:nil
cancelButtonTitle:@"取消"
otherButtonTitles:@"按鈕一", @"按鈕二", @"按鈕三",nil];
[alert show];
[alert release];
3. 如何判斷使用者點擊的按鈕
UIAlertView有一個委託UIAlertViewDelegate ,繼承該委託來實現點擊事件
標頭檔:
@interface MyAlertViewViewController : UIViewController<UIAlertViewDelegate> {
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
-(IBAction) buttonPressed;
@end
源檔案:
-(IBAction) buttonPressed
{
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:@"請選擇一個按鈕:"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"按鈕一", @"按鈕二", @"按鈕三",nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString* msg = [[NSString alloc] initWithFormat:@"您按下的第%d個按鈕!",buttonIndex];
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"提示"
message:msg
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alert show];
[alert release];
[msg release];
}
點擊“取消”,“按鈕一”,“按鈕二”,“按鈕三”的索引buttonIndex分別是0,1,2,3
4. 手動的取消對話方塊
[alertdismissWithClickedButtonIndex:0 animated:YES];
5:為UIAlertView添加子視圖
在為UIAlertView對象太添加子視圖的過程中,有點是需要注意的地方,如果刪除按鈕,也就是取消UIAlerView視圖中所有的按鈕的時候,可能會導致整個顯示結構失衡。按鈕佔用的空間不會消失,我們也可以理解為這些按鈕沒有真正的刪除,僅僅是他不可見了而已。如果在UIAlertview對象中僅僅用來顯示文本,那麼,可以在訊息的開頭添加分行符號(@"\n)有助於平衡按鈕底部和頂部的空間。
下面的代碼用來示範如何為UIAlertview對象添加子視圖的方法。
UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"請等待"
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
[alert show];
UIActivityIndicatorView*activeView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activeView.center = CGPointMake(alert.bounds.size.width/2.0f, alert.bounds.size.height-40.0f);
[activeView startAnimating];
[alert addSubview:activeView];
[activeView release];
[alert release];
6. UIAlertView預設情況下所有的text是置中對齊的。 那如果需要將文本向靠左對齊或者添加其他控制項比如輸入框時該怎麼辦呢? 不用擔心, iPhone SDK還是很靈活的, 有很多delegate訊息供調用程式使用。 所要做的就是在
- (void)willPresentAlertView:(UIAlertView *)alertView
中按照自己的需要修改或添加即可, 比如需要將訊息文本靠左對齊,下面的代碼即可實現:
-(void) willPresentAlertView:(UIAlertView *)alertView
{
for( UIView * view in alertView.subviews )
{
if( [view isKindOfClass:[UILabel class]] )
{
UILabel* label = (UILabel*) view;
label.textAlignment=UITextAlignmentLeft;
}
}
}
這段代碼很簡單, 就是在訊息框即將彈出時,遍曆所有訊息框對象,將其文本對齊屬性修改為 UITextAlignmentLeft即可。
添加其他組件也如出一轍, 如下代碼添加兩個UITextField:
-(void) willPresentAlertView:(UIAlertView *)alertView
{
CGRect frame = alertView.frame;
frame.origin.y -= 120;
frame.size.height += 80;
alertView.frame = frame;
for( UIView * viewin alertView.subviews )
{
if( ![viewisKindOfClass:[UILabelclass]] )
{
CGRect btnFrame = view.frame;
btnFrame.origin.y += 70;
view.frame = btnFrame;
}
}
UITextField* accoutName = [[UITextFieldalloc] init];
UITextField* accoutPassword = [[UITextFieldalloc] init];
accoutName.frame = CGRectMake( 10, frame.origin.y + 40,frame.size.width - 20, 30 );
accoutPassword.frame = CGRectMake( 10, frame.origin.y + 80,frame.size.width -20, 30 );
accoutName.placeholder = @"請輸入帳號";
accoutPassword.placeholder = @"請輸入密碼";
accoutPassword.secureTextEntry = YES;
[alertView addSubview:accoutPassword];
[alertView addSubview:accoutName];
[accoutName release];
[accoutPassword release];
}
顯示將訊息框固有的button和label移位, 不然添加的text field會將其遮蓋住。 然後添加需要的組件到相應的位置即可。
對於UIActionSheet其實也是一樣的, 在
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
中做同樣的處理一樣可以得到自己想要的介面。