Following Scenario:
I have a = UIViewController A that contains a UIScrollView
I have a second UIViewController "B" (amongst other things it contains a button)
and add it to the ScrollView of A
myView *mView = [[myView alloc] init];
[myScrollView addSubview:mView.view];
Is there a way that once the button is pressed that it(view) removes itself from the scrollview?
I thought in the direction of setting the delegate of mView to "A" ?! But not sure how that should work in detail....
Any suggestions?
Thanks!
In the controller, you can call:
[self.view removeFromSuperview];
If you have a reference to view you can do the following:
[view removeFromSuperview];
If you don't have a reference, please post more code and I can modify my answer for you.
Related
I need to add a subview to left part of UIViewController, which shows the user that there is something near the left part of screen, when masterview is hidden(in portrait orientation). And this view must move with left part of UISplitView. Something like the view with arrow in these two images. (Sorry for the russian interface)
http://s2.uploads.ru/8EHJI.png
http://s2.uploads.ru/NhEam.png
But my problem is that when I try to add such a view, it clips to bounds of masterview and is not visible when masterview is hidden. I think, I'm doing it wrong and there is an easy way to do this.
Update: I've tried to make some hack like:
-(void) clipToBoundsRecursive:(UIView *)someView
{
NSLog(#"%#", someView);
someView.clipsToBounds = NO;
for (UIView *v in someView.subviews)
{
[self clipToBoundsRecursive:v];
}
}
and send it to view of splitviewcontroller.
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
[self clipToBoundsRecursive:splitViewController.view];
It takes effect at first time (subviews outside the masterview's bounds shows, but after first show/hide animation they disappears and don't appear even if I call this method again)
You can add the view directly to the window's view, though you will have to manually manage its position depending on when the device rotates. Views added to the window.view will appear above the rootViewController.view.
I have described here a simple Logic for you: (how to add a UIView in Window and call it from your master or detail view )
Create UIView in #import "AppDelegate.h" ,
- (void) CreateViewInWindow
{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 780)];// set as u wish
myView.backgroundColor = [UIColor redColor];
// I have added myView to the Window with a specific animation , your can give animation as you like. :)
[UIView transitionWithView:self.window duration:0.5
options:UIViewAnimationOptionTransitionCrossDissolve // change to whatever animation you like
animations:^ { [self.window addSubview:myView]; }
completion:nil];
}
Here create a simple UIButton in the DetailViewController.m file (You can put the code in either of the files, DetailViewController.m OR MasterViewController.m)
UIButton *btnShowView = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnShowView addTarget:self action:#selector(btnShowViewTapped:) forControlEvents:UIControlEventTouchUpInside];
btnShowView.frame = CGRectMake(20, 30, 174, 35);
//[btnLoginInner setBackgroundImage:[UIImage imageNamed:#"LoginBut.png"] forState:UIControlStateNormal];
[self.view addSubview:btnShowView];
In button tapped method, write code for calling UIView from AppDelegate, and also don't forget to add #import "AppDelegate.h" in DetailViewController.m,
-(void)btnShowViewTapped:(UIButton *) Sender
{
AppDelegate *del = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[del performSelector:#selector(CreateViewInWindow) withObject:nil afterDelay:0];
}
Above code is a simple logic for your problem, It might be helpful for you.
Sounds to me like your app could benefit from PKRevealController.
Try with deselecting auto layout option in identity and type pan if you are using nib otherwise set autoresizing mask to view that currently clipping. I believe it will solve the issue
I am creating an app with multiple UIViewController, they have common menu and toolbar in header. i have created a uiviewcontroller with nib for top UIToolBar with some UIToolBarButtons. I am calling in another UIViewController, my problem is this that UIToolBar is displaying but any of UIToolbar button is not clickable now, they are not working. so it only showing the view which is not working at all.
Please see UIView UserInterfaceEnabled or not. if not then just write below code in your viewdidload
self.view.userInteractionEnabled = YES;
OR also try this
[self.view bringSubviewToFront:youtoolbar];
just try this code something skip you from your code
[yourBarButton setTarget:self];
[yourBarButton setAction:#selector(yourBtnClicked:)];
[yourToolBar setUserInteractionEnabled:YES];
[self.view setUserInteractionEnabled:YES];
[self.view bringSubviewToFront:yourToolBar];
UPDATE:
For use this UIToolBar to every UIViewController, just add it in window and set method in AppDelegate class also...
hope this help you...
Try this.. You remove your toolbar in viewWillAppear and add it again like
(void)viewWillAppear:(BOOL)animated
{
[yourtoolbar_reference removeFromSuperview];
[self.view addSubview:yourtoolbar_reference];
}
I have a ViewController which creates a NavigationController and puts a UIView in it.
nvcontrol =[[UINavigationController alloc] initWithRootViewController:menuView];
...
UIView *parent = self.view.superview;
[parent addSubview:nvcontrol.view];
I need to close the view (incl. the NavController) from within the child-viewcontroller.
The following snippet I tried, only closes the view inside the NavController, not the NavController itself. This ends up showing the blue head with a white space below.
[self.view removeFromSuperview];
What could solve this?
I have not tried this but I think something like this should work:
[self.navigationController.view removeFromSuperview];
#traingle_man Are you sure view is a property of uinavigation controller?
If I understand your question correct, this may help
[nvcontrol.view removeFromSuperview];
I'm sure this is some stupid mistake, but i'm trying for the past hour to remove a subview from my superview without any success.
On my first view i'm having
UIViewController *helpView = [[[UIViewController alloc] initWithNibName:#"HelpView" bundle:nil] autorelease];
[self.view addSubview:helpView.view];
And then inside helpView i have a button which is connected to an IBAction called "closeHelp" which just does the following:
- (IBAction) closeHelp{
[self.view removeFromSuperview];
}
But this causes my app to crash with EXC_BAS_ACCESS for some weird reason, even those this is inside the HelpView, meaning self.view should be pointed to the correct subview..
Would appreciate your help
Thank you.
Shai.
As Andreas answered, you are trying to remove self.view from its super/parent view.
You basically need to remove the helpView from its parent view.
so it should be
- (IBAction) closeHelp{
[helpView removeFromSuperview];
}
But we dont know what is "helpView" in the above method. As we dont have any handle for it.
So our code should finally look like this.
#define HELP_VIEW_TAG 101 // Give tag of your choice
HelpView *helpView = [[HelpView alloc] initWithNibName:#"HelpView" bundle:nil];
helpView.view.tag = HELP_VIEW_TAG;
[self.view addSubview:helpView.view];
[helpView release];
- (IBAction) closeHelp{
UIView *helpView = [self.view viewWithTag:HELP_VIEW_TAG];
[helpView removeFromSuperview];
}
The self.view does not point to your subview but the root view which your uiviewcontroller manages. You should probably remove only the last object in the subview stack, not the whole view, because now you are removing the whole help view.
Anyway, why do you not present the viewcontroller modally instead of doing this?
[self presentModalViewController:helpView animated:NO/YES];
helpView. modalTransitionStyle = //One of the constants below
UIModalTransitionStyleCoverVertical
UIModalTransitionStyleFlipHorizontal
UIModalTransitionStyleCrossDissolve
UIModalTransitionStylePartialCurl
Usually I am writing self.modalTransitionStyle = // One of the constants
in the viewcontroller which will be presented modally, instead of spreading the code.
You are initializing helpView as a UIViewController.
Make sure you have #import "HelpView.h" (or whatever the helpView .h file is called) in the .h file of the view controller where you are initializing it.
Then, use this code:
HelpView *helpView = [[HelpView alloc] initWithNibName:#"HelpView" bundle:nil];
[self.view addSubview:helpView.view];
That should fix it.
The easiest solution for me eventually was to just define my XIB's file owner as the same class as the parent controller, meaning the parent controller would control both the parent and the subview, which just makes a lot easier. :)
Declare the help view on calss level.
in.h file
#class HelpView;
..
#interface
{
HelpView *helpView;
}
#property(nonatomic,retain)HelpView* helpView;
In.m file
#import "HelpView"
#synthensize helpView;
now add this Code where you want
helpView = [[HelpView alloc] initWithNibName:#"HelpView" bundle:nil];
helpView.view.tag = HELP_VIEW_TAG;
[self.view addSubview:helpView.view];
- (IBAction) closeHelp{
//UIView *helpView = [self.view viewWithTag:HELP_VIEW_TAG];
[helpView removeFromSuperview];
}
-(void)dealloc
{
[helpView release];
}
I'm partially through adding a scrollview, by way of instantiating a class that subclasses uiscrollview (below called ScrollViewManager) to override touchesEnded. The problem is although my class now has scrolling and touch, I can't see my view/nib file anymore, even though it's responding to touches and scrolling fine.
My thoughts are to add back the MyClass nib as a subview? or don't know... Seems as though it's there but just hidden behind this scrollView.
The excerpt from 'Myclass : UIViewController ' has these lines of code in viewDidLoad to get the scroll action with touch response.
Thank you so much. So so much.
scrollView = [[ScrollViewManager alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(320, 600);
[scrollView setUserInteractionEnabled:TRUE];
[scrollView setScrollEnabled:TRUE];
self.view = scrollView;
//trying with this line to add my nib for this class on top of the scroll view
//which doesn't work: 'Accessing unknown 'view' class method'
[scrollView addSubview:Myclass.view];
[scrollView release];
self.view = scrollView line makes your view pointed by view controllers the scrollview.
ie self.view & scrollView now point to same object
after that when you are trying to [scrollView addSubview:Myclass.view]; what actually happening is you are adding scrollView to your scrollView & accessing view property of scrollview.
Just remove the self.view = scrollView line & do this
[self.view addSubView:scrollView];
[scrollView release];
Hopefully it'll work.
for some reason I can't add comments to your post. It didn't work.
Apple talks about adding the subview here: but doesn't explain how. Their code is below for adding the scroll aspect, as I did above. I'm now messing around with trying to add my view as a subview:
UIView *newView = [[NSBundle mainBundle] loadNibNamed:#"MyClassView" owner:self options: nil];
[scrollView addSubview:newView];
but that crashes and goes not where. Don't know what to do now. Apple's code here with original comments:
(void)loadView {
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
self.view=scrollView;
scrollView.contentSize=CGSizeMake(320,758);
scrollView.contentInset=UIEdgeInsetsMake(64.0,0.0,44.0,0.0);
// do any further configuration to the scroll view
// add a view, or views, as a subview of the scroll view.
// release scrollView as self.view retains it
self.view=scrollView;
[scrollView release];
}