Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

l0uismustdie

macrumors regular
Original poster
Nov 30, 2009
109
1
Edinburgh, UK
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
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
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?
 

l0uismustdie

macrumors regular
Original poster
Nov 30, 2009
109
1
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

macrumors regular
Original poster
Nov 30, 2009
109
1
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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
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.
 

l0uismustdie

macrumors regular
Original poster
Nov 30, 2009
109
1
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];
 

l0uismustdie

macrumors regular
Original poster
Nov 30, 2009
109
1
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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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?
 

l0uismustdie

macrumors regular
Original poster
Nov 30, 2009
109
1
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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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) ; ?
 

l0uismustdie

macrumors regular
Original poster
Nov 30, 2009
109
1
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

macrumors regular
Original poster
Nov 30, 2009
109
1
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];
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
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?
 

l0uismustdie

macrumors regular
Original poster
Nov 30, 2009
109
1
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?]
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
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?
 

l0uismustdie

macrumors regular
Original poster
Nov 30, 2009
109
1
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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
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.
 

l0uismustdie

macrumors regular
Original poster
Nov 30, 2009
109
1
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;
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
What exactly happens / are you seeing when you click the button?

Your code works fine in my test project, attached:
 

Attachments

  • MustDie.zip
    25.3 KB · Views: 73

l0uismustdie

macrumors regular
Original poster
Nov 30, 2009
109
1
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?
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.