2011年1月31日

[iPhone]NSDate和NSString的互相轉換

NSDate轉換為NSString

NSDateFormatter *date_formatter = [[[NSDateFormatter alloc]init] autorelease];
[date_formatter setDateFormat:@"YYYY/MM/dd hh:mm"];
NSString *date = [[date_formatter stringFromDate:[NSDate date]]];


NSString轉換為NSDate

NSDateFormatter *date_formatter = [[[NSDateFormatter alloc]init] autorelease];
[date_formatter setDateFormat:@"YYYY/MM/dd hh:mm"];
NSDate *date = [date_formatter dateFromString:@"2010/01/01 12:12"];

[iPhone]亂數產生NSDate物件

產生2時間內的NSDate

給定兩個NSDate物件開始日期beginDate、結束日期endDate


srandom(time(NULL));//setting random seed
NSTimeInterval interval = [endDate timeIntervalSinceDate:beginDate];
NSTimeInterval randomInterval = random()% (int)interval;
NSDate *currentDate = [beginDate dateByAddingTimeInterval:randomInterval];

給定一指定日期currentDate,產生該時間之前或之後的隨機NSDate物件
srandom(time(NULL));//setting random seed
NSTimeInterval interval = 60*60*24;//設定時間間隔的最大秒數
NSTimeInterval randomInterval = random()% (int)interval;
NSDate *randomDate = [currentDate dateByAddingTimeInterval:randomInterval];//產生currentDate之後的隨機時間

如果要產生currentDate之前的隨機時間,可以把
randomInterval加上負號,就可以產生之前的時間了。

2011年1月17日

[iPhone] 更換目前的navigation controller頁面

要怎麼更換目前的nav controller頁面呢?

首先我們先定義一個變數 *navController

UINavigationController *navContoller = [self navigationController];

接下來再進行pop和push就可以了,

[navContoller popViewControllerAnimated:NO];
[navContoller pushViewController:yourControllerName animated:NO];

2011年1月15日

[iPhone]在NSMutableArray中存取數字


要麼怎在NSMutableArray中存取數定呢?



這樣就可以把數字放到array裡囉,
[yourMutablearray addObject:[NSNumber numberWithInt:1]];

把數字從array讀取的方法是:
int num = [[yourMutablearray objectAtIndex:index] intValue];

2011年1月14日

[iPhone]在toolbar中加入Segmented Control 並置中


我想要做一個在擺放在toolbar中的segmented control,並且擺放在toolbar中央的位置。
首先我先用Interface Builder建立一個segmented control的物件並且調整它的外觀,



建立完成之後,我們在.h檔中,加入宣告
IBOutlet UISegmentedControl *modeControl;
並且定義它的property
@property (nonatomic,retain) UISegmentedControl *modeControl;

.m檔中,加入synthesize
@synthesize modeControl;


要把物件放入toolbar中,必需使用setToolbarItems:animated:這個方法(void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated
而這個方法中接收的toolbatItems array內的物件必需是UIBarButtonItem型態的。

因此,我們利用一個UIBarButtonItemsegmented control 包起來
UIBarButtonItem *barbtnShell = [[UIBarButtonItem alloc] initWithCustomView:self.modeControl];
而置中的方法是在這個 barbtnShell之前,加入一個空隙,我們可以利用UIBarButtonSystemItemFixedSpace來達到。
所以我們定義
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
並設定空隙所需要的寬度,
[space
setWidth:44.5];
接下來把空隙和segmented control加入array裡面
NSArray *array = [[NSArray alloc] initWithObjects:space,barbtnShell,nil];
最後使用setToolbarItems把物件加入toolbar
[self setToolbarItems:array animated:NO];
這樣就成功的把segmented control置中的擺放在toolbar中了。



2011年1月12日

[iPhone]設計自已的UITableViewCell


我想要做一個自已的UITableViewCell,於是在iCodeBlog找到了一篇文章"Custom UITableViewCell Using Interface Builder",上面還附有影片,可以很清楚的了解每一個步驟。

1.首先,我們先建立一個tableViewController和他的xib檔,並記得要設定section數量和列數量。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 10;
}

2.設定完成之後,新建一個Object-C class "yourStyleCell.m" subclass選項選UITableViewCell,並建立一個xib檔,
3.接下來我們使用Interface Builder,把原本的view刪除,從LibraryUITableViewCell拉進來,並且設計自已想要的樣式,然後把這個xib檔的類別設定為"yourStyleCell"
4.接下來我們在"yourStyleCell"類別中建立所需要的IBOutlet並且和xib檔做link
5.最後我們回到tableViewController.h檔中,import剛剛建立的"yourStyleCell.h"檔,
如果有變更cell的高度,請記得在tableViewControllertable也要同樣變更row的高度

接下來我們要對下面這個function進行修改
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"yourStyleCell";//這是我們在yourStyleCell.xib檔中對cell進行的定義值,
yourtStyleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//初始化cell
if (cell == nil)
{
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"yourStyleCell" owner:nil options:nil];
for ( id currentObj in nibObjs)
{
if ([currentObj isKindOfClass:[ yourtStyleCell class]])
{
cell = ( yourtStyleCell *)currentObj;
}
}
}
// 定義cell的內容
NSInteger row = [indexPath row];//get row number
// 如果是我們有一個Label,名稱為Name,我們可以這樣設定
[[cell Name] setText:@"Name"];
// 如果是我們有一個UIImageView,名稱為image,我們可以這樣設定
[[cell image] setImage:[UIImage imageNamed:@"yourImageFile.png"]];
return cell;
}


這樣就完成了我們自已stylecell了。

2011年1月8日

[iPhone]如同keyborad滑出的datepicker



我要做一個點選table
cell
之後,會像鍵盤一樣滑出的datepicker



首先我先在要顯示datepicertableview類別的.h檔中宣告了一個UIDatePicker的變數
UIDatePicker *datepicker;//代表datepicker的物件


並定義他的property
@property (nonatomic,retain) UIDatePicker *datepicker;


接下來到tableview.m檔當中synthesize
@synthesize datepicker;


要記得在最後要把這個變數release
- (void)dealloc
{
[datepicker release];
[super dealloc];
}


同時我們要定義一個記錄datepicker狀態的bool變數,用來記錄datepicker下一次要顯示還是隱藏
BOOL datepickerShow;
在 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath 
這個function中,初始化datepicker
NSInteger row = [indexPath row];
switch (row)
{
  case 0:
  if (datepicker == nil)
  //
如果datepicker還沒定義要先初始化
  {
    self.datepicker 
          [[UIDatePicker alloc]initWithFrame:CGRectMake(0.0,367,320,216)];
    //決定datepicker的初始位置,因為在我的程式裡有tabbar,因此我設定y值為367


    [self.view addSubview:datepicker];//datepicker物件加入目前的view
    datepickerShow= YES;//把狀態初始化成yes
   }
[self showDatepicker:datepickerShow];
//呼叫顯示datepickerfunction,會依照目前datepicker狀態來決定要顯示或隱藏
  break;

case 1:
[self showDatepicker:NO];//點選其他行datepicker會隱藏
  break;

case 2:
[self showDatepicker:NO];//點選其他行datepicker會隱藏
  break;
default:
  break;
}




最後加入顯示datepickerfunction
- (void)showDatepicker:(bool)show
{
 CGRect startRect = CGRectMake(0.0,367,320,216);//定義datepicker開始的位置(view外面)
 CGRect endRect = CGRectMake(0.0,171,320,216);//定義datepicker結束的位置(view裡面)
 [UIView beginAnimations:nil context:NULL];
 [UIView setAnimationCurve:5];
 if(YES==show) //若要顯示,要從view外面移進裡面
   [datepickersetFrame:endRect];
 else//view裡移到外面
   [datepicker setFrame:startRect];
 [UIView commitAnimations];
 datepickerShow =!show;//變更狀態
}




這樣就完成我們的滑動datepicker