今天介紹一下iphone中UIButton 與UITextField簡單的界面彈出對話框以及按鈕的響應 。項目需求:實現兩個按鈕 ,兩個文本框 點擊按鈕在文本輸入框中顯示從那個按鈕中點進去的信息。
?
?
聲明類
//
// testViewController.h
// test
//
// Created by 宣雨松 on 11-7-5.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
// 在ViewController中實現UIAlertViewDelegate接口 用來監聽彈出框 確定與取消
@interface testViewController : UIViewController <UIAlertViewDelegate>
{
//定義了一個按鈕buttonA
IBOutlet UIButton *buttonA;
//定義了一個文本框A
IBOutlet UITextField *textFieldA;
//定義了一個按鈕buttonB
IBOutlet UIButton *buttonB;
//定義了一個文本框B
IBOutlet UITextField *textFieldB;
}
//聲明A按鈕被按下的一個方法(IBAction) 相當于(void)
-(IBAction)bttonAPressed:(id)text;
//聲明B按鈕被按下的一個方法
-(IBAction)bttonBPressed:(id)text;
//注意這兩個方法是用來綁定在空間上 稍后我給大家介紹如何綁定
@end
?
接下來我介紹一下控件與方法的綁定 比如我須要點擊按鈕A 后調用我自己寫的方法 bttonApressed() 我需要點中按鈕后 右側出現視圖欄 點中 New Referencing Outlet 拉出一條線拖到 左側上第一個菱形上后 選 buttonA 表示這個butonA 與代碼中聲明的buttonA關聯上了 然后在點中Touch Up Inside 拉出一條線 依然拖動到左側第一個菱形上 選擇bttonAPressed()方法 這表示點擊按鈕buttonA后 會調用自己寫的方法 bttonAPressed()? 簡單吧 。 Android 開發的可視化布局卻是不如IPHONE開發的布局? J2ME 就更不行啦 哈哈( 懷念以前做J2ME游戲ing…)
?
?
實現類
//
// testViewController.m
// test
//
// Created by 宣雨松 on 11-7-5.
// Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#import "testViewController.h"
@implementation testViewController
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad]
}
*/
UIAlertView * alertA;
- (void)bttonAPressed:(id)text
{
//在這里實現了按鈕A綁定的方法
//這里說一下nil 這個東西就好比java 語言中的 null
alertA= [[UIAlertView alloc] initWithTitle:@"我的視圖" message:@"點開了A彈出對話框" delegate:self cancelButtonTitle:@"確定" otherButtonTitles: nil];
//objectiveC開發中調用方法是用"[]" 例如: [alertA addButtonWithTitle:@"取消"];
//如果是為方法賦值則類似java 對象.成員 例如 :textFieldA.text
//添加了一個取消按鈕
[alertA addButtonWithTitle:@"取消"];
//將這個UIAlerView 顯示出來
[alertA show];
//objective-C 不像java 有自己的垃圾回收機制 所以我們在編寫程序中一定要注意釋放內存 從一開始就養成良好習慣
[alertA release];
}
UIAlertView * alertB;
-(void)bttonBPressed:(id)text
{
//在這里實現了按鈕B綁定方法
alertB = [[UIAlertView alloc] initWithTitle:@"我的視圖" message:@"點開了B彈出對話框" delegate:self cancelButtonTitle:@"確定" otherButtonTitles: nil];
[alertB show];
[alertB release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//在這里添加對話框按鈕響應事件 根據不同窗口判斷
if(alertView == alertA)
{
switch (buttonIndex)
{
case 0:
textFieldA.text = @"A窗口中點擊確認按鈕";
break;
case 1:
textFieldA.text = @"A窗口點擊取消按鈕";
default:
break;
}
}else if (alertView == alertB)
{
textFieldB.text = @"B窗口點擊確定按鈕";
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
?
原文鏈接: http://blog.csdn.net/xys289187120/article/details/6586961
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

