2014-05-20 104 views
2

我在主視圖中添加了一個子視圖,但是我無法從這個視圖中刪除它。從UIViewController中刪除一個子視圖

NextViewController *nextView = [[NextViewController alloc] init]; 

nextView.transitioningDelegate = self; 
nextView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
[self.presentedViewController.view addSubview:nextView.view]; 
nextView.view.frame = CGRectMake(724, 80, 300, 150); 

UIButton *stayButton = [[UIButton alloc] initWithFrame:CGRectMake(101, 94, 90, 49)]; 
[stayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[stayButton setTitle:@"Detener" forState:UIControlStateNormal]; 
[stayButton addTarget:self 
      action:@selector(waitForNextView) 
forControlEvents:UIControlEventTouchUpInside]; 
[nextView.view addSubview:stayButton]; 
stayButton.titleLabel.font = [UIFont systemFontOfSize:25]; 
[stayButton setTitleColor:[UIColor colorWithRed:(255/255.0) green:(255/255.0) blue:(255/255.0) alpha:1] forState:UIControlStateNormal]; 

這裏我加入一個按鈕視圖,然後當按鈕被按下它goes`-

(void)waitForNextView{ 
    transition = NO; 
    NextViewController *nextView = [[NextViewController alloc] init]; 
    SectionViewController *sectionView = [[SectionViewController alloc]init]; 
    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion]; 
    actualSection = 0; 
    [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(dismissView2) userInfo:nil repeats:NO]; 
} 

我已經試過:

[nextView.view removeFromSuperView];

我不知道我還能做什麼:S

如果您需要更多信息,nextView是300x100大小的UIViewController。我想呈現它,然後在我按下按鈕時將其刪除,就這些了。

如果有人能幫助我,我會很棒。

感謝

+0

郵政dissmissView2方法,你正試圖調用[nextView.view removeFromSuperView] ; – Yan

+1

你想刪除哪個視圖?它是按鈕還是你想刪除NextView?如果NextView是一個UIViewController子類,使用'dismissViewControllerAnimated:completion:' – KerrM

回答

2

你是創建不同的引用,然後你試圖從它的超級視圖中刪除它,而根本不被添加。我會盡量解釋: 1.當你把nextView.viewself.view你已經創建了一個實例這樣

NextViewController *nextView = [[NextViewController alloc] init]; 

2.當你試圖將其刪除,而不是參照實例之上你「再在你的waitForNextView方法重新創建一個全新的,像這樣再次

NextViewController *nextView = [[NextViewController alloc] init]; 

,然後你想刪除它。請注意,nextView的最後一個實例尚未添加到任何地方,這就是您的代碼無法運行的原因。

爲了工作,你應該參考你已經添加到self.view的第一個參考。如下圖所示

在請調整你的代碼的ViewController.m

@interface ViewController() 
{ 
NextViewController *nextView; 
} 

當加入您的viewController

nextView = [[NextViewController alloc] init]; 

nextView.transitioningDelegate = self; 
nextView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
[self.presentedViewController.view addSubview:nextView.view]; 
nextView.view.frame = CGRectMake(724, 80, 300, 150); 

UIButton *stayButton = [[UIButton alloc] initWithFrame:CGRectMake(101, 94, 90, 49)]; 
[stayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[stayButton setTitle:@"Detener" forState:UIControlStateNormal]; 
[stayButton addTarget:self 
      action:@selector(waitForNextView) 
forControlEvents:UIControlEventTouchUpInside]; 
[nextView.view addSubview:stayButton]; 
stayButton.titleLabel.font = [UIFont systemFontOfSize:25]; 
[stayButton setTitleColor:[UIColor colorWithRed:(255/255.0) green:(255/255.0) blue:(255/255.0) alpha:1] forState:UIControlStateNormal]; 

最後,在你的IBAction方法

-(void)waitForNextView{ 
    transition = NO; 
    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion]; 
    actualSection = 0; 
    [nextView removeFromSuperview]; 
} 
+0

完美工作,但我已經改變了'[nextView removeFromSuperview];' '[nextView.view removeFromSuperView]; 非常感謝Panayot,你救了我:D – Norolim

+0

沒問題的人。很高興我能幫上忙 – Pancho

0

這裏的問題是,您要添加的按鈕作爲一個子視圖,以新思維的觀點,以便消除新思維的觀點是行不通的。

如果您爲您的按鈕添加標籤,那麼您可以通過使用標籤來檢索子視圖。例如。當你定義按鈕:

[stayButton setTag:1]; 
waitForNextView方法

然後,你可以得到這樣的控制:

UIButton *stayButton = (UIButton*)[nextView.view viewWithTag:1]; 
[stayButton removeFromSuperView]; 
0

使用此代碼在梅索德dismissView2

for (UIView *subview in view.subviews){ 
    [subview removeFromSuperview]; 
}