View is not switching
Hi again.
I've wrote some code to test a few stuff for a simple game but i'm stuck at one part where I made a label initialized it with CGRectMake and made a touches event: if (CGRectContainsPoint...) to work like a button.
There are two blocks on screen and one is moving horizontally.
When it collides into the other block, both are set to hidden = YES.
When one block is set to hidden, i make it so that the label appears.
When the label appears, the user should be able to touch inside it and trigger another event, which is the switchView method.
HomeView.h
HomeView.m
The problem is, it's not switching to otherView.
In MainView.m there is a function:
- (void)switchViews {
[homeView removeFromSuperview];
[self addSubview:otherView];
}
Any help would be appreciated.
Thanks.
I've wrote some code to test a few stuff for a simple game but i'm stuck at one part where I made a label initialized it with CGRectMake and made a touches event: if (CGRectContainsPoint...) to work like a button.
There are two blocks on screen and one is moving horizontally.
When it collides into the other block, both are set to hidden = YES.
When one block is set to hidden, i make it so that the label appears.
When the label appears, the user should be able to touch inside it and trigger another event, which is the switchView method.
HomeView.h
Code:
#import <UIKit/UIKit.h>
@class MainView;
@interface HomeView : UIView {
MainView *mainView;
UIImage *image;
UIImage *secondImage;
UIImageView *imageView;
UIImageView *secondImageView;
UILabel *label;
}
@property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) UIImage *secondImage;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UIImageView *secondImageView;
@property (nonatomic, retain) UILabel *label;
- (void)imageSetup;
- (void)gameOver;
@endHomeView.m
Code:
#import "HomeView.h"
#import "MainView.h"
@implementation HomeView
@synthesize image, imageView, secondImage, secondImageView;
@synthesize label;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
self.imageSetup;
}
return self;
}
- (void)imageSetup {
image = [UIImage imageNamed:@"BlockGreen.png"];
imageView = [[UIImageView alloc] initWithImage:image];
secondImage = [UIImage imageNamed:@"BlockBlue.png"];
secondImageView = [[UIImageView alloc] initWithImage:secondImage];
[self addSubview:imageView];
[self addSubview:secondImageView];
[image release];
[secondImage release];
[imageView release];
[secondImageView release];
CGPoint imageLocation = CGPointMake(50.0, 50.0);
CGPoint secondLoaction = CGPointMake(200.0, 50.0);
imageView.center = imageLocation;
secondImageView.center = secondLoaction;
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
}
- (void)gameOver {
label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 100.0, 320.0, 300.0)];
label.text = @"Game Over";
label.textAlignment = UITextAlignmentCenter;
[self addSubview:label];
}
- (void)gameLoop {
CGPoint velocity = CGPointMake(10.0, 0.0);
imageView.center = CGPointMake(imageView.center.x + velocity.x, imageView.center.y + velocity.y);
if (CGRectIntersectsRect(imageView.frame, secondImageView.frame)) {
secondImageView.hidden = YES;
imageView.hidden = YES;
}
if (secondImageView.hidden == YES) {
[self gameOver];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint(label.frame, [touch locationInView:self])) {
[mainView switchViews];
}
}
- (void)dealloc {
[label release];
[super dealloc];
}
@endThe problem is, it's not switching to otherView.
In MainView.m there is a function:
- (void)switchViews {
[homeView removeFromSuperview];
[self addSubview:otherView];
}
Any help would be appreciated.
Thanks.
Is the switchViews method being called? Use NSLog() to find out. That should help find out where the problem is.
If it is being called, check that otherView exists - something like NSLog([otherView description]) should tell you.
If it is being called, check that otherView exists - something like NSLog([otherView description]) should tell you.
hmm, i replaced [mainView switchViews] with NSLog (@"Success!") and it shows that everytime I tap "Game Over".
I must be doing something in sending the message to mainView.
I've reread my code over and over but it seems perfectly fine. =/
EDIT: I changed the code to [self removeFromSuperview]; and that goes well as when I clikc "game over" i get a black screen which is the mainview.
I must be doing something in sending the message to mainView.
I've reread my code over and over but it seems perfectly fine. =/
EDIT: I changed the code to [self removeFromSuperview]; and that goes well as when I clikc "game over" i get a black screen which is the mainview.
Here's a much simpler way of asking:
In MainView.m I have - (void)switchViews {[homeView removeFromSuperview]; [self addSubview:otherView];}
in HomeView.m I have - (void)switchViews {[mainView switchViews];}
I have imported the .h files and classes correctly cause in MainView I can initialize it with otherView and it works.
In MainView.m I have - (void)switchViews {[homeView removeFromSuperview]; [self addSubview:otherView];}
in HomeView.m I have - (void)switchViews {[mainView switchViews];}
I have imported the .h files and classes correctly cause in MainView I can initialize it with otherView and it works.
Quote:I have imported the .h files and classes correctly cause in MainView I can initialize it with otherView and it works.
Did you check otherView with the line I posted above? Check it right before you addSubview.
Depending on how you've created the view, it's possible that otherView is being released before you add it (but after you initialise mainView).
I dont seem to be getting anything with NSLog ([otherView description]);
Okay, I made another program much simpler and a bit shorter.
I think i'm having a problem with "sending the message" to another class.
In HomeView.m I call the switchViews method and send that to mainView and the swtichViews method is in the class MainView.
HomeView.m
MainView.m
The switchViews method is not in the HomeView class but in the MainView class since I want to use the MainView as a controller for other classes.
I think i'm having a problem with "sending the message" to another class.
In HomeView.m I call the switchViews method and send that to mainView and the swtichViews method is in the class MainView.
HomeView.m
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self];
if (CGRectContainsPoint(label.frame, touchLocation)) {
[mainView switchViews];
}
}MainView.m
Code:
- (void)switchViews {
[homeView removeFromSuperview];
CGRect screenBounds = [[UIScreen mainScreen] applicationFrame];
screenBounds.origin.y = 0.0;
otherView = [[OtherView alloc] initWithFrame:screenBounds];
[self addSubview:otherView];
}The switchViews method is not in the HomeView class but in the MainView class since I want to use the MainView as a controller for other classes.
jeonghyunhan Wrote:I dont seem to be getting anything with NSLog ([otherView description]);
That'll be the problem then. We'd need to see how the otherView is instantiated to find out why. It's probably just missing an [otherView retain].
So what happens when you try the new version?
Same thing.
I am not using interface builder at all either.
Would you like to see the project?
Cause i'm just stumped right now.
I am not using interface builder at all either.
Would you like to see the project?
Cause i'm just stumped right now.
Sure, I'll have a look. Can't promise anything though.
Upload it somewhere or e-mail it to max@maximile.net
Upload it somewhere or e-mail it to max@maximile.net
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Switching between UIViewContollers | mberger | 2 | 2,385 |
May 10, 2010 12:20 PM Last Post: maximile |
|
| Switching to another EAGLView help. | jeonghyunhan | 2 | 2,435 |
Jul 6, 2009 11:37 PM Last Post: AnotherJake |
|

