Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 19, 2010, 10:33 PM   #1
l0uismustdie
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
Displaying new subview once created

Hello, I am trying to display a new subview in my app. I have already created the subview and added it to the UIWindow. This is inside my appDelegate:[and as a side note if i comment out the second line the resultsController.view will display]

Code:
[window addSubview:resultsController.view];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
(resultsController.view is what I am trying to display). Once I have these views added I am trying to display resultsController.view when a button is pressed in viewController.view. Currently the button runs a function inside of the appDelegate. I have tried a number of things including:
Code:
-(IBAction)buttonPressed:(id)sender
{	
	vmAppDelegate *delegate=[vmAppDelegate new];
	
	NSLog(@"buttonpressed");
	[delegate.window addSubview:delegate.resultsController.view];	
	[delegate connectToProject:URLField.text];
}
Code:
-(void)displayNewView:(UIView*)viewToDisplay
{
	NSLog(@"displaynewview");
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:1.0];
	[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:window cache:YES];
	[window bringSubviewToFront:viewToDisplay];	
	[UIView commitAnimations];	
}
And then calling this function from inside the function called when the button is pressed. Along with a number of other ideas like adding the subview once the button has been pressed, removing the other subview when the button is pressed, etc etc... There is something I am obviously missing here and no amount of google search will discover what it is.

Thanks,
Josh
l0uismustdie is offline   0 Reply With Quote
Old Aug 20, 2010, 12:52 AM   #2
firewood
macrumors 603
 
Join Date: Jul 2003
Location: Silicon Valley
Code:
vmAppDelegate *delegate=[vmAppDelegate new];
This creates a new object. All the window and views inside it will be nil. Why do you want to play with nil objects?
firewood is offline   0 Reply With Quote
Old Aug 20, 2010, 08:57 AM   #3
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
Well obviously I don't want to "play with nil objects" that is what I am trying to learn how to change by posting my question to this forum.
l0uismustdie is offline   0 Reply With Quote
Old Aug 20, 2010, 09:06 AM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Don't use new. Seriously. Look at any sample code. It always uses alloc/init. Never new.
robbieduncan is offline   0 Reply With Quote
Old Aug 20, 2010, 09:27 AM   #5
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
Well I have looked at plenty of sample code in order to fix this problem and others I have encountered in the process. I think perhaps if you aren't going to post a constructive reply it would be more beneficial if you didn't post a reply at all. From the last post I changed the line:
Code:
vmAppDelegate *delegate=[vmAppDelegate new];
to
Code:
vmAppDelegate *delegate=[[vmAppDelegate alloc]init];
This is, I suppose, not the answer. So, to reiterate sort of exactly what I'm trying to do. I have created two views(vmViewController.view and vmController.view) in my appDelegate and added them as subviews to my UIWindow. vmViewController.view has a button that calls a function in the appDelegate and I am trying to get the second view to become the top view once that function is called.

Thanks again
l0uismustdie is offline   0 Reply With Quote
Old Aug 20, 2010, 09:37 AM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
No, that's what you think you've done. What you are really doing is creating a new instance of the app delegate class instead of using the already existing one. This is why it's not working. Which is what you were told in the first reply.
robbieduncan is offline   0 Reply With Quote
Old Aug 20, 2010, 09:48 AM   #7
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Also, the usual approaches to showing a new view are either presenting it modally or pushing it onto a navigation stack.

I guess I will channel chown33 here and ask what is your experience level with iPhone programming and programming in general? What books or tutorials have you already learned from? Be specific.
__________________
dejo is offline   0 Reply With Quote
Old Aug 20, 2010, 09:52 AM   #8
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
Well you've made your point...I don't know how to solve this problem. But that's why I posted here. I am not sure how to use the already existing instance of the app delegate without creating one. This is the next idea that I've tried:
Code:
vmAppDelegate *appDelegate=[[UIApplication sharedApplication]delegate];
[appDelegate connectToProject:URLField.text];

Last edited by l0uismustdie; Aug 20, 2010 at 10:02 AM.
l0uismustdie is offline   0 Reply With Quote
Old Aug 20, 2010, 10:06 AM   #9
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by l0uismustdie View Post
This is the next idea that I've tried...
...and?... what was the result?
__________________
dejo is offline   0 Reply With Quote
Old Aug 20, 2010, 10:07 AM   #10
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
Haha, sorry. Figured you would sense my lack of enthusiasm. This still doesn't allow me to pull the subview to the front when the button is pushed. Additionally the second view is loading (viewDidLoad is running) it is just not being displayed. The two lines I am running are:
Code:
[viewController.view removeFromSuperview];
[window addSubview:resultsController.view];
I have also tried this without the removeFromSuperview and still have not been able to display the view.
l0uismustdie is offline   0 Reply With Quote
Old Aug 20, 2010, 10:14 AM   #11
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by l0uismustdie View Post
Haha, sorry. Figured you would sense my lack of enthusiasm. This still doesn't allow me to pull the subview to the front when the button is pushed.
Bringing a subview to the front (aside: are you even sure you want a subview at this point; see my previous post on showing new views) is something quite different from adding a subview, which you are actually doing twice for resultsController.view (once in applicationDidFinishLaunching, I'm guessing, and again in buttonPressed:).

Where have you learned about showing new views?
__________________
dejo is offline   0 Reply With Quote
Old Aug 20, 2010, 10:24 AM   #12
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
The only thing I really know about views I have gathered from assorted internet sites. This is the last portion of a project I have been working on all summer and I am pretty far away from an OO programmer. I spend most of my time at as low of a level as I can be. All I really need to do is create a welcome screen that allows the user to type in a URL (which I have done) and then once they click the connect button switch to a new view where I can just display some information as things are happening throughout my program. As for adding the view twice I had actually commented out the first one (applicationDidFinishLaunching and placed it in the function the button is calling. As for your last post it is a modal window I am looking for because at this stage I don't care if the user can get back to the "welcome screen". In my program, in order:
Code:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{       
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}
Code:
-(IBAction)buttonPressed:(id)sender
{	
	vmAppDelegate *appDelegate=[[UIApplication sharedApplication]delegate];
	[appDelegate connectToProject:URLField.text];	
}
Code:
-(void)connectToProject:(NSString *)URL
{	NSLog(@"urlField,%@",URL);
	[window addSubview:resultsController.view];
	while(1) ;
}
It is inside of this function I have been doing most of my experimenting. This is obviously where I want the next view (resultsController.view) to be displayed.
l0uismustdie is offline   0 Reply With Quote
Old Aug 20, 2010, 10:30 AM   #13
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by l0uismustdie View Post
As for your last post it is a modal window I am looking for because at this stage I don't care if the user can get back to the "welcome screen".
So, you should now have enough hints to begin searching around for how to present a modal view from within your desired method.

P.S. What's with the while(1) ; ?
__________________
dejo is offline   0 Reply With Quote
Old Aug 20, 2010, 10:44 AM   #14
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
What I am still confused on is why in applicationDidFinishLoading: can I write
Code:
[window addSubview:resultsController.view];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[window bringSubviewToFront:resultsController.view];
and see the resultsController.view but I can't do this same thing in a later function in the same class? That seems the easiest solution to the problem.
l0uismustdie is offline   0 Reply With Quote
Old Aug 20, 2010, 10:52 AM   #15
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by l0uismustdie View Post
...but I can't do this same thing in a later function in the same class
Is your connectToProject: still just adding the subview or is it now calling bringSubviewToFront: ?

P.S. I still think, based on your requirements, you should pursue the modal presentation approach.
__________________

Last edited by dejo; Aug 20, 2010 at 11:07 AM.
dejo is offline   0 Reply With Quote
Old Aug 20, 2010, 11:35 AM   #16
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
You are probably right that I should take that approach. I just don't understand why this approach doesn't work the way I think it should. my connectToProject: has used a number of different techniques including adding the subView and then bringing it to the front and removing the previous subView. I have also tried this function I found someone use for this particular idea:
Code:
-(void)displayNewView:(UIView*)viewToDisplay
{
	NSLog(@"displaynewview");
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:1.0];
	[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:window cache:YES];
	[window bringSubviewToFront:viewToDisplay];	
	[UIView commitAnimations];	
}
Additionally I have now tried this line (commented out all the others) in connectToProject:
Code:
[viewController presentModalViewController:resultsController animated:YES];

Last edited by l0uismustdie; Aug 20, 2010 at 11:50 AM.
l0uismustdie is offline   0 Reply With Quote
Old Aug 20, 2010, 11:59 AM   #17
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
So, I'm guessing (again!) that none of these attempts is working. And by "not working", I mean: compiles fine, no warnings or errors, runs okay (other than not doing what you want), no run-time errors or crashes. Is that right? And you're certain connectToProject: is being called and resultsController is non-nil and hasn't been released?
__________________
dejo is offline   0 Reply With Quote
Old Aug 20, 2010, 12:10 PM   #18
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
That's right...the results haven't changed. In response to your last post though I tried three changes, the first being sending a retain message to responseController inside of applicationDidFinishLaunching
Following that I tried (inside of connectToProject:
Code:
[viewController presentModalViewController:resultsController animated:YES];
Code:
[window addSubview:resultsController.view];
and
Code:
[window addSubview:resultsController.view];
[window bringSubviewToFront:resultsController.view];
I am certain that connectToProject: is being called as for resultController I am also certain (simple did a NSLog("%@",resultsController) inside of connectToProject: and it is still around) [Side note: Is there a way to check a variable's retain count during execution?]
l0uismustdie is offline   0 Reply With Quote
Old Aug 20, 2010, 12:25 PM   #19
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
So, resultsController is non-nil, viewController is non-nil, you call presentModalViewController:animated: and nothings happens? Something screwy is going on in your project!

Anyways, how is resultsController declared and where is it instantiated?
__________________
dejo is offline   0 Reply With Quote
Old Aug 20, 2010, 12:32 PM   #20
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
Yeah I am with you there. I have done everything the same between viewController and resultsController. It was created with a xib file. Here is how everything is declared:
Code:
@interface vmAppDelegate : NSObject <UIApplicationDelegate> 
{
	UIWindow				*window;
	vmViewController		*viewController;
	vmController			*resultsController;
	NSString				*dataFile,*r,*savedURL;
}
@property(nonatomic,retain)IBOutlet	vmController	 *resultsController;
@property(nonatomic,retain)IBOutlet	vmViewController *viewController;
Its next appearance in the program (same as with viewController) is when they are added as subViews to window.

Also, I sincerely appreciate your help today.
l0uismustdie is offline   0 Reply With Quote
Old Aug 20, 2010, 12:38 PM   #21
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
How did you connect resultsController to the IBOutlet?

Which is totally unnecessary, IMHO. Here's what I would do:
Code:
-(void)connectToProject:(NSString *)URL
{	NSLog(@"urlField,%@",URL);
	ResultsController *resultsController = [[ResultsController alloc] init];
	// do something with URL here probably
	[viewController presentModalViewController:resultsController animated:YES];
	[resultsController release];
}
No property, no ivar, no adding to subview, just a simple alloc/init when needed and use right away.
__________________
dejo is offline   0 Reply With Quote
Old Aug 20, 2010, 01:01 PM   #22
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
Excellent. I think we are heading on the right track. I have implemented what you suggested and still have no luck. So here are some ideas. I am certain the view is loading (NSLog(@"viewDidLoad") inside of viewDidLoad: ) After that a function inside of vmController.h gets called. Well maybe this is easier...here is the code:
Code:
- (void)viewDidLoad 
{
    [super viewDidLoad];
	NSLog(@"vmControllerDidLoad");
	[self setuptextView];
}
-(void)setuptextView
{
	NSLog(@"set up text view");
	self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease];
	self.textView.textColor = [UIColor blackColor];
	self.textView.font = [UIFont fontWithName:@"Arial" size:18];
	self.textView.delegate = self;
	self.textView.backgroundColor = [UIColor whiteColor];
	
	self.textView.text = @"This is the text view example,we can edit , delete, add content in the text view.";
	self.textView.returnKeyType = UIReturnKeyDefault;
	self.textView.keyboardType = UIKeyboardTypeDefault;
	self.textView.scrollEnabled = YES;
	
	
	self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
	[self.view addSubview: self.textView];  
}
and here is vmController.h
Code:
@interface vmController : UIViewController <UITextViewDelegate>
{
	IBOutlet UITextView		*eventView;
	UITextView				*textView;
}
@property(nonatomic,retain)UITextView	*textView;

-(void)setuptextView;
l0uismustdie is offline   0 Reply With Quote
Old Aug 20, 2010, 01:19 PM   #23
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
What exactly happens / are you seeing when you click the button?

Your code works fine in my test project, attached:
Attached Files
File Type: zip MustDie.zip (25.3 KB, 25 views)
__________________
dejo is offline   0 Reply With Quote
Old Aug 20, 2010, 01:41 PM   #24
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
When the button is clicked nothing happens really. The NSLog() events are printed and then the program hits the while(1) ; loop. Yours does work though so I am examining some differences. Just out of curiosity what type of project did you create? View based?
l0uismustdie is offline   0 Reply With Quote
Old Aug 20, 2010, 01:46 PM   #25
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by l0uismustdie View Post
When the button is clicked nothing happens really. The NSLog() events are printed and then the program hits the while(1) ; loop.
Ah, well, there's your problem! What the heck is that supposed to be doing (other than blocking the UI loop)? Get rid of it!

Maybe we could've gotten to the issue earlier, if you had responded to my query about it when I asked.
__________________
dejo is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
custom UITableViewCell subview frame erdinc27 iPhone/iPad Programming 5 May 21, 2014 01:21 AM
arrays? subview or submenu? tacotester1 iPhone/iPad Programming 6 Mar 14, 2014 08:06 PM
Custom NSView with subview of NSSplitView ArtOfWarfare Mac Programming 3 Aug 20, 2013 05:16 AM
Can't Add Subview!?! Blakeasd iPhone/iPad Programming 2 Jan 5, 2013 05:41 PM
Subview is not working in UIScrollView Narendar Singh iPhone/iPad Programming 0 Nov 23, 2012 07:49 AM

Forum Jump

All times are GMT -5. The time now is 10:21 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC