removing Views from SubView

时间秒杀一切 提交于 2019-12-11 16:03:40

问题


I have a App that uses the In App feature.

-(void) completeTransaction: (skPaymenttransaction *)transaction{



}

When the above method gets called i want to remove all subviews and go back to my main menu window (the first view in my App).

Can anyone suggest to cleanest and best way to do this?

Cheers

EDIT:

Just to make things clear

Im not sure if this makes a difference but i have my main menu screen, then iam doing the following with an enter button.

UIViewController *controller = [[UIViewController alloc] initWithNibName:@"NibFile" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];

Then i have a main screen with a button then when a user taps it, it then presents them with another modal view controller as above. On this view is a button that says BUY on it. They use clicks this and then the StoreKit does it business and once the payment is complete i want to get rid of the two modal controllers above and be left with the main menu screen.

Any Ideas.. ive tried as above

EDIT 2:

@Jordan Thanks,

But not sure if im doing this correctly. I understand the above code.

But when i start my app my app delegate loads a viewcontroller which is my main menu. Then i have a button that takes me to another view and on there is my features if the user clicks a feature that is not unlocked then it displays another view controller with the store on.

So with this in mind how do i get back to my main menu.

I have tried the following:

NSArray *subviews = [myAppdelegate.viewcontroller.view subviews]; 
for (int i=0; i<[subviews count]; i++) 
{ 
   [[subviews objectAtIndex:i] removeFromSuperview]; 
} 

but i get and error along the lines of:

expected ':' before '.' ?


回答1:


This should work.

// view is equal to your main view    
NSArray *subviews = [view subviews];
for (int i=0; i<[subviews count]; i++)
{
   [[subviews objectAtIndex:i] removeFromSuperview];
}



回答2:


If you're talking about UIViewControllers and not subViews (they are different),then you can use:

 [self.navigationController popToRootViewControllerAnimated:YES];

You're either adding UIViews to a UIViewController, in which case use my code above, or You're pushingViews (e.g. pushViewController) on top of a UIViewController, in which case use the code here.




回答3:


We have to get the views to be removed in an array so that we can remove everything by means of enumeration

 NSArray *ChildViews = [ParentView subviews];
 for (UIView *childView in ChildViews) {
   [childView removeFromSuperview];
 }


来源:https://stackoverflow.com/questions/2416674/removing-views-from-subview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!