본문 바로가기

개발도구/iOS - 아이폰 개발

[아이폰] 기본 기능 function

*************************************************************************
1. 초기화/제목달기
*************************************************************************

- (id)init
{
     self = [super init];
     if(self != nil)
     {
          //     initialize
     }
     return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
     if(self == [super initWithCoder:aDecoder])
     {
          self.title = @"첫번째 ^^";
     }
     return self;
}

*************************************************************************
2. 테이블뷰
*************************************************************************

///////////////////////////////////////////////////

//     Table View Template

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

     return 5; // return [listData count];

}


//Cell정의 (Cell돌려줌)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

     //cell id있어야함

     static NSString *cIdentifier = @"cell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cIdentifier];

     //셀 값이 없고 재활용 가능한 셀이면 재활용하고 아니면 다시만든다.

     if(cell == nil)

     {

          cell = [[[UITableViewCell alloc

          initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cIdentifier]autorelease];

//initWithStyle:UITableViewCellStyleValue1reuseIdentifier:cIdentifier]autorelease];
          }

     if(indexPath.row == 0)

     {

          cell.textLabel.text =@"First Row";

//cell.detailTextLabel.text = @"detail 0";
          }

     else 

     {

          cell.textLabel.text =@"Other Cell";

//cell.detailTextLabel.text = @"detail 1";
     }

     return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

*************************************************************************
3. 피커뷰
*************************************************************************

1) 아웃렛을 선언하고 데이터를 받을 어레이를 만든다.

IBOutlet UIPickerView *picker;

NSMutableArray *pickData;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {

     pickData = [[NSMutableArray alloc]init];

     [pickData addObject:@"1"];

     [pickData addObject:@"2"];

     [pickData addObject:@"3"];

     [pickData addObject:@"4"];

     [pickData addObject:@"5"];

     [pickData addObject:@"6"];

     [super viewDidLoad];

}

2) 데이터 소스와 델리게이트 생성
// data source

// Picker View 개수

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

     return 1;

}


// Picker View 데이터 개수

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

     return [pickData count];

}


// delegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

     // NSLog(@"value : %i", row);

     return [pickData objectAtIndex:row];

}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

     // 선택되었을시의 처리

     //label.text = [pickDataobjectAtIndex:row];

}

*************************************************************************
4. 뷰전환
*************************************************************************
뷰콘트롤러 하나에 뷰가 여러개 있을 때
- 최상위 뷰를 제거하고 새 뷰를 넣는다.
- (IBAction)ChangeView
{
     NSArray *subs = [windows subviews];
     [[subs objectAtIndex:0] removeFromSuperview];
     if([[subs objectAtIndex:0] == secondView)
     {
          [window addSubview:firstView];
     }
}

뷰콘트롤러 자체가 여러개 있을 때
- 루트뷰 콘트롤러 자체를 변경한다.

rootView *rView = [[rootView alloc]initWithNibName:@"rootView" bundle:nil];

self.view.window.rootViewController = rView;
[rView release];

내비게이션 콘트롤러에서의 뷰전환
1) push(front)
- 내비게이션 콘트롤러의 뷰콘트롤러를 설정한다.

UINavigationController *curNav = self.navigationController;

NewAccountView *viewControl = [[NewAccountView alloc]initWithNibName:@"NewAccountView" bundle:nil];

[curNav pushViewController:viewControl animated:YES];

[viewControl release];

2) pop(back) - 화면전환이 없을시에만 가능하다. 화면전환이 있다면..

UINavigationController *curNav = self.navigationController;

[curNavpopViewControllerAnimated:YES];

모달뷰인경우 
1) push

UINavigationController *curNav = self.navigationController;

[curNav presentModalViewController:thePlayer animated:NO]; 

2) pop
[self.navigationControllerdismissModalViewControllerAnimated:NO];

*************************************************************************
5. xib파일간의 데이터 전달
*************************************************************************

1) 전달받을 데이터 (SecondView)

@interface SecondView : UIViewController {

     NSString *receiveData; // FirstView 에서 넘어올 데이터

}

@property (readwriteassign) NSString *receiveData;
@synthesize receiveData;

2) 전달받는 곳 (SecondView)

- (void)viewDidLoad {

lbltext.text = receiveData;

    [superviewDidLoad];

}

3) 전달하는 곳(FirstView)
// NavigationController인 경우

UINavigationController *sNav = self.navigationController;

SecondView *sView = [[SecondView alloc]initWithNibName:@"SecondView" bundle:nil];

sView.receiveData = textData.text;

[sNav pushViewController:sView animated:YES];

[sView release];

// View인 경우

rootView *rView = [[rootViewalloc]initWithNibName:@"rootView"bundle:nil];

self.view.window.rootViewController = rView;
[rView release];


*************************************************************************
6. Alert
*************************************************************************
1) UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

     NSLog(@"Clicked Button index : %i", buttonIndex);

}


-(IBAction)clickSimple

{

     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"제목message:@"내용을적으면됨delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK버튼", nil];

     [alert show];

     [alert release];

}


-(IBAction)clickMulti

{

     UIAlertView *malert = [[UIAlertView alloc]initWithTitle:@"멀티제목message:@"내용을적으셈delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK버튼"@"Cancel"nil];

     [malert show];

     [malert release];

}

2) UIActionSheetDelegate
// action sheet
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

     NSLog(@"Action Sheet Index : %i\n", buttonIndex);

}

- (IBAction)clickActionSheet

{

     UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"제목"delegate:self cancelButtonTitle:@"취소destructiveButtonTitle:@"포커스otherButtonTitles:@"천재"@"위대함"nil];

     [actionSheet showInView:[self view]];

     [actionSheet release];

}

*************************************************************************
7. ETC 콘트롤 : segmented, switch, slider, progress
*************************************************************************
1) secgmented control - |a|b|c|
인스펙터의 속성 : bar는 얇다, 매 세그먼트의 이름을 정해준다.
단순 IBAction과 동일하다. 단 sender를 사용한다.

- (IBAction) segmentChange:(id)sender

{

     NSLog(@"click Index: %i", [sender selectedSegmentIndex]);

}

2) switch control - [on|off]

- (IBAction) switchChange:(id)sender

{

     UISwitch *tmpSwitch = (UISwitch*)sender;

     if(tmpSwitch.isOn)

     {

          NSLog(@"On");

     }

     else 

     {

          NSLog(@"Off");

     }

}


3) slider control

- (IBAction) sliderChange:(id)sender

{

     UISlider *tmpSlider = (UISlider *)sender;

     NSLog(@"value : %f", tmpSlider.value);//0.0~1.0까지입니다.

}

4) progress view control : 아웃렛이 존재
IBOutlet UIProgressView *nowProgress;
단순하게 progress 접근자에 값만 세팅하면 변한다.

- (IBAction) click3

{

     nowProgress.progress = 1.0;

}

*************************************************************************
8. UITextField에서 키보드 감추기
*************************************************************************

- (IBAction)hiddenKeyboard

{

     NSLog(@"hidden keyboard");

     NSArray *subs = self.view.subviews;

     id curView;

     for(curView in subs)

     {

          if([curView conformsToProtocol:@protocol(UITextInputTraits)])

          {

               [curView resignFirstResponder];

          }

     }

}

*************************************************************************
9. 애니메이션
*************************************************************************
1) 아래에서 위로 올라가기 효과 (모달뷰)
가기

Modal *modalView = [[Modal alloc]initWithNibName:@"Modal" bundle:nil];

[self presentModalViewController:modalView animated:YES];
돌아오기
[self dismissModalViewControllerAnimated:YES];

2) 좌우로 돌아가기 효과
가기

LeftTurn *lrView = [[LeftTurnalloc]initWithNibName:@"LeftTurn" bundle:nil];

[UIView beginAnimations:@"left flip" context:nil];

[UIView setAnimationDuration:1.5];

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRightforView:self.viewcache:YES];

[self.viewaddSubview:lrView.view];

[UIViewcommitAnimations];

돌아오기

[UIView beginAnimations:@"back"context:nil];

[UIView setAnimationDuration:0.5];

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeftforView:self.view.superviewcache:YES];

[self.viewremoveFromSuperview];

[UIViewcommitAnimations];

3) 페이지 넘기기 효과
가기

PageFlip *crView = [[PageFlipalloc]initWithNibName:@"PageFlip"bundle:nil];

[UIViewbeginAnimations:@"page flip"context:nil];

[UIViewsetAnimationDuration:1.5];

[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUpforView:self.viewcache:YES];

[self.viewaddSubview:crView.view];

[UIViewcommitAnimations];
돌아오기

[UIViewbeginAnimations:@"back"context:nil];

[UIViewsetAnimationDuration:0.5];

[UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIViewsetAnimationTransition:UIViewAnimationTransitionCurlDownforView:self.view.superviewcache:YES];

[self.viewremoveFromSuperview];

[UIViewcommitAnimations];

*************************************************************************
10. 달력
*************************************************************************

// 현재 달력을 얻는다.

NSCalendar *calendar = [NSCalendar currentCalendar]; // 오늘달력을 얻는다.

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;

NSDate *date = [NSDate date]; // 오늘 날짜를 얻는다.

NSDateComponents *comp = [calendar components:unitFlags fromDate:date];

NSLog(@"yeay:month:day = %d:%d:%d", [comp year], [comp month], [comp day]);

NSDateComponents *comps = [[NSDateComponentsallocinit];

[comps setMonth:20];

[comps setYear:1000];

NSDate *newDate = [calendar dateByAddingComponents:comps toDate:date options:0];

comp = [calendar components:unitFlags fromDate:newDate];

NSLog(@"yeay:month:day = %d:%d:%d", [comp year], [comp month], [comp day]);

[comps release];