2013-02-08 83 views
15

我有一個按鈕,我在視圖控制器中以編程方式創建。一旦按下按鈕,我希望它使用一種方法以編程方式創建彈出窗口。如何以編程方式從也是以編程方式創建的uibutton以編程方式顯示彈出窗口(不使用接口生成器)

其在viewDidLoad中創建的我認爲Controller.m或者

UIView *moreFundInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 540, 620)]; 
[self.view addSubview:moreFundInfoView]; 
[moreFundInfoView setBackgroundColor:[UIColor RMBColor:@"b"]]; 

btnContact = [UIButton buttonWithType:(UIButtonTypeRoundedRect)]; 


[btnContact setFrame:CGRectMake(390, 575, contactButton.width, contactButton.height)]; 
btnContact.hidden = NO; 
[btnContact setTitle:@"Contact" forState:(UIControlStateNormal)]; 
[moreFundInfoView addSubview:btnContact]; 

[btnContact addTarget:self action:@selector(showContactDetails:) forControlEvents:UIControlEventTouchUpInside]; 

按鈕然後,我有該按鈕被按下時,我使用的方法。

-(void) showContactDetails: (id) sender 
{ 
UIViewController *popoverContent = [[UIViewController alloc]init]; 

UIView *popoverView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 300)]; 

[popoverView setBackgroundColor:[UIColor RMBColor:@"b"]]; 

popoverContent.view = popoverView; 

popoverContent.contentSizeForViewInPopover = CGSizeMake(200, 300); 

UIPopoverController *contactPopover =[[UIPopoverController alloc] initWithContentViewController:popoverContent]; 

[contactPopover presentPopoverFromRect:btnContact.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES ]; 

[contactPopover setDelegate:self]; 

}

缺少什麼我在這裏?導致它運行良好,但只要我點擊按鈕應用程序崩潰。我認爲這是一個代表問題,但我不確定。任何意見,將不勝感激。

+1

你能給我們碰撞記錄嗎?這將有助於瞭解問題出在哪裏 –

+0

@ValentinRocher仍然是n00b的一部分,但這是崩潰時的輸出。 appname- [UIPopoverController dealloc]在popover仍然可見的情況下到達。[uipopoverController dealloc]到達時彈出窗口仍然可見。 – Berns

+0

嗨,我設法修復它,無論如何,太好了。我只是將UIPopoverController的屬性更改爲強壯,並且它可以工作,只是它指向下而不是向上,但它至少顯示並且應用程序不會崩潰。 – Berns

回答

1

我所要做的只是在.h文件中將屬性從「retain」更改爲「strong」,並且它可以正常工作,從而避免應用程序崩潰。

+4

如果這樣做解決了你的問題,你可以接受你自己的答案。 – Vineeth

32

我認爲這段代碼會幫助你。你肯定缺少委託方法

ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navigationController]; 
popover.delegate = self; 
popover.popoverContentSize = CGSizeMake(644, 425); //your custom size. 
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections: UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp animated:YES]; 

只要確保你不會忘記UIPopover委託方法,否則應用程序肯定會崩潰。這是強制性的。

+3

使用UINavigationController是完全可選的。您可以跳過其對象的創建。僅供參考。有一個快樂的編碼。 –

1

是的,將「retain」屬性更改爲「strong」使您可以保留拾取器視圖對象。 我認爲你的代碼有問題,UIPopoverController對象在方法完成時會自動釋放。 使強壯的財產讓你強烈指向一個對象。

2
UIViewController *controller = [[UIViewController alloc] init]; 
[view removeFromSuperview]; //view is a view which is displayed in a popover 
controller.view = view; 
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:controller]; 
popover.delegate = self; 
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES]; 
相關問題