Getting Subview from another Subview - objective-c

I have my main window's content view set like this:
newContentView = [[CutoutView alloc]initWithFrame:window.frame];
[window setContentView:newContentView];
[newContentView release];
Where CutoutView is the name of my subclass. Now I want to add a subview to it so I did the following,
filterView = [[FilterView alloc]initWithFrame:NSMakeRect(100, 100, 500, 500)];
[newContentView addSubview:filterView];
[filterView release];
that all works fine except now I want to call methods from my filterView subclass and I want to get it like this but it wont work.
FilterView *filter = [[NSApp delegate] contentView];
I read in the docs that by using contentView it only "returns the highest accessible NSView object in the window hierarchy" So I tried adding the following to the addSubview
[newContentView addSubview:filterView positioned:NSWindowAbove relativeTo:nil];
but that didnt work either any ideas as to what I need to do? Thanks!

The content view is really your CutoutView class so you should be using:
FilterView *filterView = [[[[[NSApp delegate] window] contentView] subviews] objectAtIndex:0];
But two cleaner ways are:
1) Use IBOutlets to keep track of your views and assign them via IB.
2) Use tags:
filterView.tag = 101;
then use:
FilterView* filterView = [[[NSApp delegate] contentView] viewWithTag:101];

ContentView is a method of your window not the app delegate, so if you have a window whose IBOutlet is "window" in your app delegate, then you need to use: FilterView *filter = [[[NSApp delegate] window] contentView];

Related

Custom segue, but leave the previous scene showing

Imagine a custom segue ...
-(void)perform
{
UIView *sv = ((UIViewController *)self.sourceViewController).view;
UIView *dv = ((UIViewController *)self.destinationViewController).view;
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
[window insertSubview:dv aboveSubview:sv];
[dv coverFromRight:0 then:^
{
[self.sourceViewController
presentViewController:self.destinationViewController
animated:NO completion:nil];
}];
}
Which in fact, only PARTIALLY (!) covers the "underneath, previous" scene,
and in fact DOES NOT call "presentViewController", so, the "underneath, previous" scene in fact keeps operating normally.
-(void)perform
{
UIView *sv = ((UIViewController *)self.sourceViewController).view;
UIView *dv = ((UIViewController *)self.destinationViewController).view;
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
[window insertSubview:dv aboveSubview:sv];
[dv coverButOnlyHalfWay:0 then:^
{
}];
}
Essentially, is this possible?
In fact, I've found from experiment the above works (!!). BUT when you come to the custom unwind segue, it does not work: everything crashes. (Perhaps as you'd expect.)
What's the situation? is there a way to make a custom segue, which, covers only say half the "original, underneath" scene and leaves that scene running?
(I appreciate you can just implement this using a container view, but it's not as clean as a whole segue scene.)
Why use a segue? You can just add your view as a subview and position it correct using CGRectMake, this would be much easier.
// Size Your View with X, Y coordinates
[viewController.view setFrame:CGRectMake(0, 0, 320, 192)];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
[self addChildViewController:viewController];

How do I Insert View in Superview's View? (iOS) [duplicate]

I have the line of code below... How would I modify it to insert the subview in the superview's view named 'foo'?
[[self superview] addSubview:copy3];
There are more than 1 ways of doing this -
1.have an IBOutlet reference in your code. This is created from xcode Interface builder. Once you have this then it fairly straight forward -
[iboutlet_foo_object addSubview:copy3];
2.tag the view that you are interested in. Again tagging a view can be done from xcode Interface builder or from code. After that -
Foo *fooObj = [[self superview] viewWithTag:tagInt];
[fooObj addSubview:copy3];
3.Finally you can iterate through all the views in your superview & see which one is of type Foo -
NSArray *subviews = [[self superview] subviews];
for(UIView *v in subviews)
{
if(v isKindOfClass:[Foo class])
{
[v addSubview:copy3];
break;
}
}
hope these methods help you...
First make myView into a property of the superview
Then use
[[[self superview] myView] addSubview:mySubview];
First tag the view myView with a unique number:
myView.tag = 0x1234;
Then you can find it using viewWithTag:
[[[self superview] viewWithTag:0x1234] addSubview:mySubview];
You might just actually add it to 'view' instead of 'superview'.
[[self view] addSubview:mySubview];
It works just fine for me, whenever I add subviews to my parent view.
Let me know if something went wrong.

How to Insert View in Superview's View? (iOS)

I have the line of code below... How would I modify it to insert the subview in the superview's view named 'foo'?
[[self superview] addSubview:copy3];
There are more than 1 ways of doing this -
1.have an IBOutlet reference in your code. This is created from xcode Interface builder. Once you have this then it fairly straight forward -
[iboutlet_foo_object addSubview:copy3];
2.tag the view that you are interested in. Again tagging a view can be done from xcode Interface builder or from code. After that -
Foo *fooObj = [[self superview] viewWithTag:tagInt];
[fooObj addSubview:copy3];
3.Finally you can iterate through all the views in your superview & see which one is of type Foo -
NSArray *subviews = [[self superview] subviews];
for(UIView *v in subviews)
{
if(v isKindOfClass:[Foo class])
{
[v addSubview:copy3];
break;
}
}
hope these methods help you...
First make myView into a property of the superview
Then use
[[[self superview] myView] addSubview:mySubview];
First tag the view myView with a unique number:
myView.tag = 0x1234;
Then you can find it using viewWithTag:
[[[self superview] viewWithTag:0x1234] addSubview:mySubview];
You might just actually add it to 'view' instead of 'superview'.
[[self view] addSubview:mySubview];
It works just fine for me, whenever I add subviews to my parent view.
Let me know if something went wrong.

Second UIWindow Not Displaying (iPad)

I am attempting to create two UIWindows because I would like two UINavigationControllers on screen at the same time on my app. I initialize two windows in my app delegate but only one window's view is displayed. Does anyone know why this is so?
Here is the code I used:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIViewController * controller1 = [[UIViewController alloc] init];
[controller1.view setBackgroundColor:[UIColor grayColor]];
UINavigationController * nav1 = [[UINavigationController alloc] initWithRootViewController:controller1];
[window addSubview:nav1.view];
[window makeKeyAndVisible];
UIWindow * window2 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
UIViewController * controller2 = [[UIViewController alloc] init];
[controller2.view setBackgroundColor:[UIColor yellowColor]];
UINavigationController * nav2 = [[UINavigationController alloc] initWithRootViewController:controller2];
[window2 addSubview:nav2.view];
[window2 makeKeyAndVisible];
NSLog(#"%#", [[UIApplication sharedApplication] windows]);
return YES;
}
The gray from the first window is visible, but the yellow from the second is not. The output from this is:
"<UIWindow: 0x591e650; frame = (0 0; 768 1024); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x591e7a0>>",
"<UIWindow: 0x5923920; frame = (0 0; 100 100); layer = <CALayer: 0x59239a0>>"
which means the second window is created and added to the application, but just not displayed. Does anyone know why this is so?
Thanks in advance!
The two UIWindow's windowLevel property is equal, they are all UIWindowLevelNormal.
If you want the second UIWindow display font of the first UIWindow, You should set the second UIWindow's windowLevel value bigger. Like:
window2.windowLevel = UIWindowLevelNormal + 1;
PS:
[window makeKeyAndVisible];
...
[window2 makeKeyAndVisible];
There is only one keyWindow at a time, The key window is the one that is designated to receive keyboard and other non-touch related events. Only one window at a time may be the key window.
Just use a UISplitViewController.
Or try MGSplitVIewController if you need to customization. It might have what you need.
I've discovered how to get the second UIWindow to display. You must set the clipsToBound property to YES. Otherwise, the view from one of the windows will completely cover the other view. The two windows were properly added and visible after all.
This might be a really old post but I just run into the same problem. Some coding mistakes where already answered but the main issue we have here is how you instantiating the UIWindow.
Here is a Swift example how to display another UIWindow correctly.
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? = UIWindow(frame: UIScreen.mainScreen().bounds)
let newWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
// save a reference to your Window so it won't be released by ARC
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window!.rootViewController = SomeViewController()
self.window!.makeKeyAndVisible()
// in your example you have created the window inside this method,
// which executes correctly and at the end of this method just releases the window,
// because you never saved the reference to the window
self.newWindow.rootViewController = SomeOtherViewController()
self.newWindow.windowLevel = UIWindowLevelStatusBar + 1.0
self.newWindow.hidden = false
return true
}
}
Btw. you don't have to create a UIWindow in AppDelegate. It depends on your code behavior.
try this code...
id delegate = [[UIApplication sharedApplication] delegate];
[[delegate FirstView] presentModalViewController:SecondView animated:YES];

UIView transition and animation

I understand modal views cover the entire screen. But I really want a view that covers only half the screen just like the keyboard. So, please tell me why this doesn't work
MyController *controller = [[MyController alloc] initWithNibName:#"MyView" bundle:nil];
CGRect frame = CGRectMake(0,44,768,264);
[controller view].frame = frame;
contoller.delegate = self;
[[self view] addSubView:[controller view]];
[controller release];
I am trying to add a sub view to my current view and make it appear where the keyboard appears.
It throws a BAD ACCESS exception
In my code (above), I was using a custom UIViewController with it's own view [set to UIView on IB]. I couldn't get it to work by setting frame for the view controller's view.
So I added a custom UIView without a Nib file with all the controls (buttons, textfields) added on initWithFrame.
MyCustomView = [[MyCustomView] alloc] initWithFrame:frame delegate:self];
[self.view addSubView:MyCustomView];
Thanks for your comment, Jacob.