2013-02-20 45 views
2

在我的應用程序中,我想要一個自定義彈出窗口,當從UIButton調用時出現。目前我正在使用TSAlertView,但我想實現更多的UI項目並且有更多的自定義構建。所以我想要做的是在另一個視圖控制器中加載視圖控制器或視圖,並能夠在它們之間傳遞信息。看下面的例子:如何將ViewController或View加載到具有透明背景的另一個ViewController中?

ViewController

從我讀過的東西,據說它是不是在另一個加載另一個視圖 - 控制很好的做法。在這種情況下,我會假設一個uview是推薦的方式。它是否正確?

我希望彈出窗口後面的viewcontroller具有暗淡的外觀,也許黑色的不透明度設置爲70%。我怎樣才能做到這一點?

基本上,我試圖建立一個類似於AlertView與另一個視圖的外觀。

+0

這是爲iPhone或iPad? – 2013-02-20 19:31:59

回答

1

如果您打算完全阻止背景視圖,您可以將viewControllerB設置爲Form Sheet

當你準備好展示viewControllerB,使用方法:

[viewControllerA presentViewController:viewControllerB animated:TRUE completion:nil] 

您需要一種方法來解散viewControllerB;一個按鈕,網絡動作等

與故事板的一個例子:

- (void)someMethod { 
    UIStoryboard *storyboard = self.storyboard; 
    ViewControllerB *viewCotrollerB = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerB"]; 
    [self presentModalViewController:viewControllerB animated:YES]; 
} 
+0

感謝您的快速回復。我仍然在iOS開發中學習新東西,那麼如何將viewController設置爲iphone故事板中的表單?並通過在viewController B中設置視圖清除將允許我看到viewController一個正確的? – SReca 2013-02-20 19:27:33

+0

@SReca我想你將不得不保留那個故事板,並創建在不同的.xib文件和相關的視圖控制器。 – 2013-02-20 19:30:53

+0

@SReca查看更新。 – 2013-02-20 19:35:26

0

enter image description here

SReca您好,我也具有同樣的要求(如在上面的圖片示出)以類似UIAlertView中創建視圖你在說什麼。

在這裏你將不得不創建一個新的UIView類而不是UIViewController,而不需要.xib。

在init方法中的新UIView類中,您將需要以編程方式創建用戶界面。並儘可能保持其高度寬度。

對於加載這個新的UIView,你將不得不創建該uiview的對象,並調用initwithframe它將以uialerview的形式以編程方式呈現該視圖。

這裏是參考代碼。

ModalPreviewViewController_iPhone.h

@interface ModalPreviewViewController_iPhone : UIView 
@end 

ModalPreviewViewController_iPhone。米

只是修改此示例代碼,根據自己的需要在initWithFrame

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     popUpView = [[UIView alloc] initWithFrame:CGRectMake(30,30,260,420)]; 

     popUpBgView = [[UIView alloc] init]; 
     popUpBgView.frame =CGRectMake(0,0,260,420); 
     [popUpBgView setBackgroundColor:[UIColor colorWithRed:51.0/255 green:51.0/255 blue:51.0/255 alpha:1]]; 
     [popUpView addSubview:popUpBgView]; 


     imageGalleryView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 260, 360)]; 
     [imageGalleryView setBackgroundColor:[UIColor redColor]]; 

     CGRect btnTempFrame=CGRectMake(5, 385, 90, 30); 
     btnTemp=[[UIButton alloc]initWithFrame:btnTempFrame]; 
     [btnTemp setBackgroundImage:[UIImage imageNamed:@"btn_plain_iphone.png"] forState:UIControlStateNormal]; 
     [btnTemp.titleLabel setFont:[UIFont boldSystemFontOfSize:13]]; 
     [btnTemp.titleLabel setTextColor:[UIColor whiteColor]]; 
     [btnTemp addTarget:self action:@selector(onDownloadClick:) forControlEvents:UIControlEventTouchUpInside]; 

     CGRect btnSubscribeFrame=CGRectMake(100, 385, 90, 30); 
     UIButton *btnSubscribe=[[UIButton alloc]initWithFrame:btnSubscribeFrame]; 
     [btnSubscribe setBackgroundImage:[UIImage imageNamed:@"btn_plain_iphone.png"] forState:UIControlStateNormal]; 

     [btnSubscribe.titleLabel setFont:[UIFont boldSystemFontOfSize:13]]; 
     [btnSubscribe.titleLabel setTextColor:[UIColor whiteColor]]; 
     [btnSubscribe setTitle:@"Subscribe" forState:UIControlStateNormal]; 
     [btnSubscribe addTarget:self action:@selector(moveTosubscribe:) forControlEvents:UIControlEventTouchUpInside]; 

     CGRect closeButtonFrame=CGRectMake(195, 385, 60, 30); 
     UIButton *closeButton=[[UIButton alloc]initWithFrame:closeButtonFrame]; 
     [closeButton setBackgroundImage:[UIImage imageNamed:@"btn_plain_iphone.png"] forState:UIControlStateNormal]; 
     [closeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13]]; 
     [closeButton.titleLabel setTextColor:[UIColor whiteColor]]; 
     [closeButton setTitle:@"Close" forState:UIControlStateNormal]; 
     [closeButton addTarget:self action:@selector(closeView:) forControlEvents:UIControlEventTouchUpInside]; 

     CGRect swipeLabelFrame=CGRectMake(0, 0, 140, 15); 
     UILabel *swipeLabel=[[UILabel alloc]initWithFrame:swipeLabelFrame]; 

     [swipeLabel setBackgroundColor:[UIColor clearColor]]; 
     [swipeLabel setText:@"> SWIPE TO SEE MORE"]; 
     [swipeLabel setTextColor:[UIColor lightGrayColor]]; 
     [swipeLabel setFont:[UIFont systemFontOfSize:12]]; 

     CGRect dateLabelFrame=CGRectMake(5, 365, 100, 15); 
     dateLabel=[[UILabel alloc]initWithFrame:dateLabelFrame]; 

     [dateLabel setBackgroundColor:[UIColor clearColor]]; 
     [dateLabel setText:@"July 2013 #93"]; 
     [dateLabel setTextColor:[UIColor whiteColor]]; 
     [dateLabel setFont:[UIFont systemFontOfSize:13]]; 

     CGRect priceLabelFrame=CGRectMake(155, 365, 100, 15); 
     priceLabel=[[UILabel alloc]initWithFrame:priceLabelFrame]; 

     [priceLabel setBackgroundColor:[UIColor clearColor]]; 
     [priceLabel setText:@"$3.9"]; 
     [priceLabel setTextColor:[UIColor colorWithRed:71.0/255 green:191.0/255 blue:226.0/255 alpha:1]]; 
     [priceLabel setFont:[UIFont systemFontOfSize:13]]; 
     [priceLabel setTextAlignment:NSTextAlignmentRight]; 



     CGRect labelFrame=swipeLabel.frame; 
     labelFrame.origin=CGPointMake(popUpView.frame.origin.x+popUpView.frame.size.width - labelFrame.size.width, popUpView.frame.origin.y - labelFrame.size.height); 


     [swipeLabel setFrame:labelFrame]; 

     [popUpView addSubview:imageGalleryView]; 
     [popUpView addSubview:btnTemp]; 
     [popUpView addSubview:closeButton]; 
     [popUpView addSubview:btnSubscribe]; 
     [popUpView addSubview:dateLabel]; 
     [popUpView addSubview:priceLabel]; 

     [UIView beginAnimations:@"curlup" context:nil]; 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDuration:.5]; 
     [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:popUpView cache:YES]; 
     [self addSubview:popUpView]; 

     [self addSubview:swipeLabel]; 
     [UIView commitAnimations]; 

     UIColor *color = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]; 
     self.backgroundColor = color; 

     [[[UIApplication sharedApplication] keyWindow] addSubview:self]; 
     [self initializeVariables]; 
    } 
    return self; 
} 

而且在父視圖控制器只需創建UIView類的對象,並使用initWithFrame和大小要

ModalPreviewViewController_iPhone *mpvc=[ModalPreviewViewController_iPhone alloc]; 

     if(appDelegate.isIphone5) 
      mpvc=[mpvc initWithFrame:CGRectMake(0,0,320,480)]; 
     else 
      mpvc=[mpvc initWithFrame:CGRectMake(0,0,320,568)]; 
叫它

而對於解僱UivewClass

寫下面的句子來解僱它,在它的一個像(UIviewClass的)方法closeView

[self removeFromSuperview]; 

讓我知道,如果你面對任何問題

相關問題