2014-08-31 29 views
0

我正在嘗試將條形碼掃描API添加到我的iPad應用程序(XCode5,iOS7,Storyboards)。儘管代碼已被執行,但我似乎無法使其工作。爲了簡潔起見,可以找到完整的掃描碼here。這個問題似乎是從掃描類這段代碼:爲什麼在嘗試將UIPopover附加到不同類中的UIButton時會出現此錯誤?

// define the window 
_highlightView = [[UIView alloc] init]; 
_highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin; 
_highlightView.layer.borderColor = [UIColor greenColor].CGColor; 
_highlightView.layer.borderWidth = 3; 
[self.view addSubview:_highlightView]; 

認爲我需要的是在它小的UIView一個UIPopover,因爲沒有出現在我目前的UIView。所以這是我想出了代碼:

UIViewController* popoverContent = [[UIViewController alloc] init]; 
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 350, 500)]; 
popoverView.backgroundColor = [UIColor colorWithWhite:(CGFloat)1.0 alpha:(CGFloat)1.0]; // frame color? 
popoverContent.view = popoverView; 

//resize the popover view shown in the current view to the view's size 
popoverContent.preferredContentSize = CGSizeMake(350, 500); 

_highlightView = [[UIView alloc] init]; 
_highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin; 
_highlightView.layer.borderColor = [UIColor greenColor].CGColor; 
_highlightView.layer.borderWidth = 3; 
[self.view addSubview:_highlightView]; 

// if previous popoverController is still visible... dismiss it 
if ([popoverController isPopoverVisible]) { 
    [popoverController dismissPopoverAnimated:YES]; 
} 

//create a popover controller 
DetailViewController *dvc = [[DetailViewController alloc]init]; 

popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent]; 

[popoverController presentPopoverFromRect:dvc.oReadBarCode.frame 
           inView:self.view 
      permittedArrowDirections:UIPopoverArrowDirectionUp 
          animated:YES]; 

不過,我得到一個生成錯誤的最後一行,我presentPopoverFromRect定義按鈕附加酥料餅來:

Popovers不能從沒有窗戶的視角呈現。「

我該如何解決這個問題? (這個想法是讓UIPopover顯示掃描結果)。

回答

1

您不需要將框架投射到按鈕......加上您的括號似乎已關閉。

你可以簡單地這樣做:

[popoverController presentPopoverFromRect:dvc.oReadBarCode.frame 
            inView:self.view 
       permittedArrowDirections:UIPopoverArrowDirectionUp 
           animated:YES]; 

編譯器的投訴本身(備查)似乎是因爲你想投一個結構的指針,它無法做到的。

+0

你在做這些代碼之前做了什麼?爲了防止錯誤,將所有內容都包含在'if(self.view.window){/ * code * /}' – nhgrif 2014-08-31 19:22:25

+0

因此,所有這些代碼都在'viewDidLoad'中?將其移至'viewWillAppear:'或'viewDidAppear:'。 – nhgrif 2014-08-31 19:27:58

+0

一個視圖在它作爲子視圖添加到具有窗口的視圖之前沒有窗口,或者如果它是一個視圖控制器的'.view'屬性,那麼它將不會在' viewDidLoad'(我認爲),它將不會有一個窗口,直到'viewWillAppear:'或'viewDidAppear:'。 – nhgrif 2014-08-31 19:31:02

相關問題