2012-02-22 1 views
0

나는 다음과 같은 코드 팝 오버를 만들려고 해요는 : 는 UIPopoverController 문제

//.h 
@interface LoginViewController : UIViewController <UIPopoverControllerDelegate> 
.... 
@end 

//.m 
- (IBAction)popoverTest:(id)sender 
{ 
    UIViewController *popoverContent = [[UIViewController alloc] init]; 
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; 

    UILabel *nameLabel = [[UILabel alloc] init]; //edited, fixed UILabel allocation 
    nameLabel.text = @"Person's name"; 
    [nameLabel sizeToFit]; 
    nameLabel.frame = CGRectMake(10, 10, nameLabel.frame.size.width, nameLabel.frame.size.height); 
    [myView addSubview:nameLabel]; 

    popoverContent.view = myView; 

    popoverContent.contentSizeForViewInPopover = CGSizeMake(300, 300); 

    UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent]; 
    popoverController.delegate = self; 

    [popoverController presentPopoverFromRect:((UIButton *)sender).frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 

    NSLog(@"ran all code"); 
} 

는 I는 UIView을 만든 하위 뷰으로 라벨을 부착 한 후 UIViewController.view에 내 시야를 할당. 그런 다음 Popover 컨트롤러를 만들고 Popover 컨트롤러의 크기를 지정하고 대리자를 설정 한 다음 단추의 프레임에서 표시합니다.

SIGABRT가 표시되고 앱이 다운됩니다.

내가 누락 된 자료가 있습니까?

편집 : UILabel 할당을 수정했습니다. 문제는 항상 있습니다.

+0

IBAction 내에 중단 점을 넣고 충돌 지점을 확인하십시오. 또한 [예외]를 가능하게하십시오 (http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4) –

답변

3

코드가 매우 이상합니다.

예를 들어보기를 만들고이를 팝 오버의 컨텐츠보기에 추가하는 이유는 무엇입니까?

그런 다음 메모리 누수에주의해야합니다. 귀하의 코드에는 많은 것들이 있습니다.

즉, 여기에 을 표시하기위한 간단한 예가 UIPopoverController입니다. self.popover이 유지 정책과 @property입니다

- (IBAction)yourAction:(id)sender 

    UIButton* senderButton = (UIButton*)sender; 

    UIViewController* yourController = [[UIViewController alloc] init]; 

    UIPopoverController* pop = [[UIPopoverController alloc] initWithContentViewController:yourController]; 
    pop.delegate = self; 
    pop.popoverContentSize = CGSizeMake(300, 300);  

    self.popover = pop; 

    [pop presentPopoverFromRect:senderButton.bounds inView:senderButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
    pop.passthroughViews = nil; 

    [yourController release]; 
    [pop release]; 
} 

. 이 방식으로 UIPopoverControllerDelegate 메쏘드 (또는 원하는 곳 어디에서든지)에서 여러분은 여러분의 popover를 해제 할 수 있고 /거나 닫을 수 있습니다.

희망이 있습니다.

P. 내가 직접 작성했기 때문에 코드를 확인하십시오. 당신이 팝 오버를 만들 때

편집

보통, 컨텐츠 뷰 컨트롤러 또는 사용자 정의 UIViewController 또는 UINavigationController (이 경우에 당신은 탐색 모음을 활용하려는).예를 들어

대신 간단한 UIViewController의, 사용자 정의 하나

//.h 
@interface CustomUIViewController : UIViewController 

@end 

//.m 
@implementation CustomUIViewController 

// other code here... 

- (void)viewDidLoad 
{ 
    // here you are sure that the view has been loaded in memory (alternatively override loadView method), so 
    UIView* greenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; 
    green.backgroundColor = [UIColor greenColor]; 
    [self.view addSubview:greenView]; 
    [greenView release]; 
} 

@end 

을 만들 수 있고

- (IBAction)yourAction:(id)sender 

    UIButton* senderButton = (UIButton*)sender; 

    CustomUIViewController* yourController = [[CustomUIViewController alloc] init]; 

    // same as before... 
} 
+0

제안 해 주셔서 감사합니다. 그리고 나는 내가이 프로젝트에서 ARC를 사용하고 있다고 말 했어야했다. – Padin215

+0

답을 다시 읽고 내 UIPopoverController를 문제를 해결 한 인스턴스 변수로 만들 것을 제안 했음 – Padin215

+0

안녕하십니까. –

0

문제는 다음과 같은 것 같다 :

UILabel *nameLabel; 
nameLabel.text = @"Person's name"; 

nameLabel가 초기화되지 않은 것입니다. 랜덤 메모리를 가리키며 텍스트 속성을 통해 -setText:을 호출하면 응용 프로그램이 충돌합니다.

이 외에도 몇 가지 메모리 누수가 있습니다. 기억하십시오 : alloc으로 전화를 걸면 release 어딘가에 있습니다. (새로운 자동 할당/릴리스에서 이것이 어떻게 작동하는지 모르겠습니다).

+0

오, 그래, 나는 그것을 "UILabel * nameLabel = [[UILabel alloc] init]; " 그러나 그것을 고치지 않았다. – Padin215

1

from this question.

짧은 대답 내 문제 해결 AA 팝 오버 내에서 사용 : ARC는 UIPopoverController를 유지하지 않습니다.

나는 그것을 인스턴스 변수로 만들었고 잘 동작합니다.