아이폰 생성관련 코드 정리한것입니다 많이 보세요[펌글]

버튼 : (viewDidLoad)에써줌

-(void)viewDidLoad {
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(80, 50, 150, 100)];
[self.view addSubview:button];
[button setTitle:@"자동차" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];//이벤트사용시
}

ex) window-based에서 버튼생성시

UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(47, 362, 60, 35)];//[[UIButton buttonWithType:UIButtonTypeCustom]retain];
[button.titleLabel setTextColor:[UIColor blackColor]];
[button setTitle:@"자동차" forState:UIControlStateNormal];
button.titleLabel.textColor = [UIColor blackColor];
[self.view addSubview:button];

레이블 : (viewDidLoad)에써줌

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(20, 30, 100, 150)];
label.text = @"텍스트";
[self.view addSubview:label];
[label release];

뷰(viewDidLoad)에써줌

UIView *view1=[[UIView alloc]initWithFrame:CGRectMake( 0, 0,300,400)];
view1.backgroundColor=[UIColor redColor];
[self.view addSubview:view1];

alertview프로토콜 : <UIAlertViewDelegate>적용

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"내가만든것" message:@"조심해" delegate:self
cancelButtonTitle:@"확인" otherButtonTitles:@"취소",nil];   //메서드에 써줌
[alert show];
[alert release];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 0)
[label setText:@"확인을 누르셨습니다"];
else
[label setText:@"취소를 누르셨습니다"];
}

ActionSheet 프로토콜 : <UIActionSheetDelegate>적용

-(void)click2{
NSLog(@"두번째 버튼을 누르셨습니다");
UIActionSheet *action = [[UIActionSheet alloc]initWithTitle:@"다이얼로그" delegate:self cancelButtonTitle:nil
destructiveButtonTitle:nil otherButtonTitles:@"확인", @"취소", @"재시도", nil];
[action showInView:self.view];
[action release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if(buttonIndex==0)
label.text=@"확인";
else if(buttonIndex ==1)
label.text =@"cancel";
else
label.text =@"재시도";
}

이미지뷰 생성 : (loadView)에씀

[super loadView];
UIImage *img = [UIImage imageNamed: @"image2.png"];                          // 불러올 이미지 파일 가져옴
UIImageView *imageView = [[UIImageView alloc] initWithImage:img];     // 이미지 파일을 뷰에 붙임
[imageView setUserInteractionEnabled:YES];                                       // 이미지뷰가 인터렉션 할 수 있게
[img release];
self.view = imageView;               // 이미지뷰를 현재 뷰에 붙임
[imageView release];

스크롤뷰 생성해서 이미지출력 : 프로토콜<UIScrollViewDelegate>적용

UIImage *img = [UIImage imageNamed: @"image2.png"];                          // 불러올 이미지 파일 가져옴
UIImageView *imageView = [[UIImageView alloc] initWithImage:img];     // 이미지 파일을 뷰에 붙임
[imageView setUserInteractionEnabled:YES];                                       // 이미지뷰가 인터렉션 할 수 있게
CGSize imgSize = [img size];                                                // 현재 불러온 이미지의 크기 확인
[img release];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];       // 스크롤뷰 생성
[scrollView setScrollEnabled:YES];    // 스크롤뷰 스크롤 가능하게
[scrollView setContentSize:imgSize];   // 스크롤뷰의 크기 설정
[scrollView addSubView:imageView];  // 위에서 만든 이미지뷰를 스크롤뷰에 올림
[imageView release];// 이미지뷰 없앰 (다 사용했으므로...)
self.view=scrollView;
_imageView=imageView;//인스턴스 멤버변수쓰는공간에 "UIImageView *_imageView;"만듬, 확대/축소를한 출력이미지를 _imageView에 넣음
[scrollView setMaximumzoomScale:2.0f];      // 스크롤뷰의 최대 확대 크기 지정
[scrollView setMinimumzoomScale:0.5f];      // 스크롤뷰의 최소 축소 크기 지정
[scrollView setDelegate:self];                      // 델리게이트 설정
// 스크롤뷰를 현재 뷰에 붙임
[scrollView release];
 // 이 메서드는 줌을 사용할 뷰를 반환해야 합니다
- (UIView *)viewForZoomingInScrollView:(UIScrollView *) scrollView    //확대축소관련 메서드 델리게이트를 꼭지정해야함
{
return _imageView;
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale//줌을 한 이 후의 동작에 대해서 지정해 줄 수 있습니다.

view 붙이기 : loadView에 씀

UIScreen *screen = [UIScreen mainScreen];        // 자기 자신(MyUIView) 스크린 생성
CGRect frame = [screen applicationFrame]   ;     // 그릴 스크린의 프레임 얻기  bounds 는 전체 사이즈, applicationFrame은 어플리케이션 사이즈
UIView *myUIView = [[UIView alloc] initWithFrame:frame];    // 프레임 크기에 MyUIView 영역 초기화
myUIView.backgroundColor = [UIColor blueColor];    // MyUIView의 배경색 설정
self.view = myUIView;        // 현재의 뷰 연결, 화면에 설정한 뷰 나타남
[myUIView release];

참고 : http://cafe.naver.com/mcbugi/111755

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s