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

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
Hi everyone,

I would like to know how to refresh a view?

In my SecondView has accelerometer objects and also have some removeFromSuperview, and when I enter the SecondView from MainView again, the removeFromSuperview objects are not there anymore. I need a refresh thing to make all the objects start all over again on SecondView.

code in .h file:
Code:
@interface SecondView : UIViewController <UIAccelerometerDelegate> {
......................
}

code in .m file:
Code:
- (IBAction)done {
    [self.parentViewController dismissModalViewControllerAnimated:YES];
    [B][SecondView reloadData];[/B]
}

I added the reloadData, but it shows the warning: "SecondView" may not respond to "+reloadData" :confused:

Hope anyone can help, really thank you. :(
 
Last edited:

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
Hi everyone,

I would like to know how to refresh a view?

In my SecondView has accelerometer objects and also have some removeFromSuperview, and when I enter the SecondView from MainView again, the removeFromSuperview objects are not there anymore. I need a refresh thing to make all the objects start all over again on SecondView.

code in .h file:
Code:
@interface SecondView : UIViewController <UIAccelerometerDelegate> {
......................
}

code in .m file:
Code:
- (IBAction)done {
    [self.parentViewController dismissModalViewControllerAnimated:YES];
    [B][SecondView reloadData];[/B]
}

I added the reloadData, but it shows the warning: "SecondView" may not respond to "+reloadData" :confused:

Hope anyone can help, really thank you. :(

reloadData is for UITableViews, which u probably don't have.
u need something like this
[self.view setNeedsDisplay];
or
[self.view setNeedsLayout];
 

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
reloadData is for UITableViews, which u probably don't have.
u need something like this
[self.view setNeedsDisplay];
or
[self.view setNeedsLayout];
hi jnoxx,

I already added the line that you mentioned. But I'm not sure why it exits the apps when I press the "enter" button on Main page.:( I put the code on my "enter" button. "secondView" is my second page. And goes like this:

Code:
-(IBAction)switchViews {
	[self presentModalViewController:secondView animated:YES];
	[B][secondView setNeedsDisplay];[/B]
}
 

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
Is secondView a UIView or a UIViewController in this case? Did you get any warnings when you tried to Build that code?
Hi dejo,

In my SecondView.h
Code:
@interface SecondView : UIViewController <UIAccelerometerDelegate> {
......
}

In my main page TwoViewAppViewController.h
Code:
#import <UIKit/UIKit.h>
@class SecondView;
@interface TwoViewAppViewController : UIViewController {
	IBOutlet SecondView *secondView;
}

when I simulate it and click on the ENTER button on main page, it leads me to my xcode window and I see this "TERMINATING DUE TO UNCAUGHT EXCEPTION" next to debugger console button.
 

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
Again, I ask: Did you get any warnings when you tried to Build that code?

Code:
-(IBAction)switchViews {
	[B][self presentModalViewController:secondView animated:YES];[/B]
}

Before I put the refresh view code, I already have this warning. But I didn't bother, coz it can go to second page with no errors.

The warning that I get is(bold text above): passing argument 1 of 'presentModalViewController:animated:' from distinct Objective-C type
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Well, you should understand and, almost certainly, fix your warnings before expecting your code to work as hoped.

P.S. With that said, I still don't think that code is going to do what you'd like it to do.
 

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
Well, you should understand and, almost certainly, fix your warnings before expecting your code to work as hoped.

P.S. With that said, I still don't think that code is going to do what you'd like it to do.
Thanks, then do you have any idea what does the warning mean? :confused:
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Thanks, then do you have any idea what does the warning mean? :confused:
What type of object is presentModalViewController: expecting and what type of object are you passing it. Think about that.

Also, what have you done already to figure out the meaning of that warning?
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
The usual meaning of that warning is that the specified parameter isn't of the type that is found in the prototype. So if the prototype calls for an NSString* and the actual type of the parameter is a NSDictionary* you would get the warning.

You should always fix this warning. Occasionally you would fix it with a typecast but usually it indicates that you've made an error.
 

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
What type of object is presentModalViewController: expecting and what type of object are you passing it. Think about that.

Also, what have you done already to figure out the meaning of that warning?

Thanks dejo and PhoneyDeveloper. I already solved the warning. On my second page, I'm missing the #import SecondView.h.

After adding this:
Code:
-(IBAction)switchViews {
	[self presentModalViewController:secondView animated:YES];
	[B][secondView setNeedsDisplay];[/B]
}
Now I get a warning: 'SecondView' may not respond to '-setNeedsDisplay'
 
Last edited:

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
setNeedsDisplay is a UIView method. If your secondView is a view controller then it won't respond to that method, hence the warning.

You haven't explained this problem very well. What kinds of views need to be redisplayed? Standard controls won't do anything useful with setNeedsDisplay. It's a red herring. Obviously view controllers will just crash if you send them a message they don't implement.

Try to explain in more detail what is supposed to happen. What is the view hierarchy? What is happening? What kinds of views are supposed to reload their info?
 

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
setNeedsDisplay is a UIView method. If your secondView is a view controller then it won't respond to that method, hence the warning.

You haven't explained this problem very well. What kinds of views need to be redisplayed? Standard controls won't do anything useful with setNeedsDisplay. It's a red herring. Obviously view controllers will just crash if you send them a message they don't implement.

Try to explain in more detail what is supposed to happen. What is the view hierarchy? What is happening? What kinds of views are supposed to reload their info?
Hi PhoneyDeveloper,

By looking at my SecondView.h file as follow, I think it's using view controller
Code:
@interface SecondView : UIViewController <UIAccelerometerDelegate> {
................
}
The program goes like this:
1st view: I have a ENTER button(UIButton/round rect button) which enters the second view.

2nd view: Contains an accelerometer ball to touch two objects(when it touches the box, it will remove the box from superview). One BACK button(UIBarButtonItem) and one START button(UIBarButtonItem). When press start then it loads the accelerometer function to let the ball moves.

If I didn't refresh it, when I click BACK to 1st view then ENTER to 2nd view again. The 2nd view still remains the same, I would like the 2nd view back to the beginning where I click on the START button then it moves, later on I wanna add SCORE also to record scores, then if I BACK to 1st then ENTER 2nd view, the score will reset zero again, just everything start all over again on 2nd view.

Just now I tried to put this, all succeeded but nothing happens on 2nd view:confused::
Code:
[B][secondView.view setNeedsDisplay];[/B]

Hope my explanation clear enough, if I miss something, please advice me. Thank you :)
 
Last edited:

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Based on your description of the flow, I would recommend that you instantiate a new SecondView every time you move from your first view to your second (i.e. in your switchViews method), like so:
Code:
-(IBAction)switchViews {
    secondView = [[SecondView alloc] init];
    [self presentModalViewController:secondView animated:YES];
}
[NOTE: This code is oversimplified and doesn't deal with leaking of secondView, etc.]

This will ensure that when your second view is presented, it's all fresh and in a "start" state.

And perhaps you could explain the reason for secondView being an IBOutlet-connected instance variable in your TwoViewAppViewController.

P.S. As a hint for your understanding of the classes you've setup, you might want to consider renaming SecondView (which might be mistaken for a UIView subclass) as SecondViewController instead. Such a naming convention is fairly common in iPhone dev.
 

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
Based on your description of the flow, I would recommend that you instantiate a new SecondView every time you move from your first view to your second (i.e. in your switchViews method), like so:
Code:
-(IBAction)switchViews {
    secondView = [[SecondView alloc] init];
    [self presentModalViewController:secondView animated:YES];
}
[NOTE: This code is oversimplified and doesn't deal with leaking of secondView, etc.]

This will ensure that when your second view is presented, it's all fresh and in a "start" state.

And perhaps you could explain the reason for secondView being an IBOutlet-connected instance variable in your TwoViewAppViewController.

P.S. As a hint for your understanding of the classes you've setup, you might want to consider renaming SecondView (which might be mistaken for a UIView subclass) as SecondViewController instead. Such a naming convention is fairly common in iPhone dev.
Hi dejo,

Actually these codes and the naming I followed a tutorial from YouTube video.

I thought I have secondView being an IBOutlet-connected instance variable in my TwoViewAppViewController is to link to secondView? If I didn't put it says undeclared. :confused:

As you said, now I already changed all the names "SecondView" to "SecondViewController", including the header names and etc.

If I already changed the names then it become like this:
Code:
-(IBAction)switchViews {
    secondViewController = [[SecondViewController alloc] init];
    [self presentModalViewController:secondViewController animated:YES];
}

Then I simulate it, and when I click on the ENTER button, it shows a blank view. So do you mean I need to create another empty UIView in InterfaceBuilder to link it to the first view?

sorry for my poor understanding :(. If I get it wrong, please correct me, thank you.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Actually these codes and the naming I followed a tutorial from YouTube video.
What YouTube tutorial? Being specific about where you are getting your help from helps us.

I thought I have secondView being an IBOutlet-connected instance variable in my TwoViewAppViewController is to link to secondView? If I didn't put it says undeclared. :confused:
And you're actually connecting it to a viewController in Interface Builder?


As you said, now I already changed all the names "SecondView" to "SecondViewController", including the header names and etc.

If I already changed the names then it become like this:
Code:
-(IBAction)switchViews {
    secondViewController = [[SecondViewController alloc] init];
    [self presentModalViewController:secondViewController animated:YES];
}

Then I simulate it, and when I click on the ENTER button, it shows a blank view. So do you mean I need to create another empty UIView in InterfaceBuilder to link it to the first view?
No, I don't mean create another empty UIView. Does your SecondViewController have an associated UIView? That is, did you specify the option "With XIB for user interface" when creating it?
 

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
What YouTube tutorial? Being specific about where you are getting your help from helps us.
I created as the tutorial from this YouTube link:
http://www.youtube.com/watch?v=sCZhDOgVFNA

And you're actually connecting it to a viewController in Interface Builder?
From the connections there in InterfaceBuilder, I drag it to my viewController like what he did in video link above.


No, I don't mean create another empty UIView. Does your SecondViewController have an associated UIView? That is, did you specify the option "With XIB for user interface" when creating it?
Yes, I don't know why in the video tutorial above each UIView has viewController.

I don't know how to specify "with XIB for user interface" actually:(, I see him just drag the viewController, then drag the UIView. Meaning first view has a viewController and a UIView, second view has a viewController and a UIView.

Or should I paste the complete code here?
 
Last edited:

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
First view header - TwoViewAppViewController.h
Code:
#import <UIKit/UIKit.h>

@class SecondViewController;
@interface TwoViewAppViewController : UIViewController {
	IBOutlet SecondViewController *secondViewController;
}

-(IBAction)switchViews;

@end

First view main - TwoViewAppViewController.m
Code:
#import "TwoViewAppViewController.h"
#import "SecondViewController.h"

@implementation TwoViewAppViewController

-(IBAction)switchViews {
	[self presentModalViewController:secondViewController animated:YES];
}

- (void)viewDidLoad {
}

- (void)didReceiveMemoryWarning {
       [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {	
}

- (void)dealloc {
    [super dealloc];
}

@end

Second view header - SecondViewController.h
Code:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

#define BALL_RADIUS 25

@class ThirdViewController;
@class TwoViewAppViewController;
@interface SecondViewController : UIViewController <UIAccelerometerDelegate> {
	
	IBOutlet ThirdViewController *thirdViewController;
	IBOutlet TwoViewAppViewController *twoViewAppViewController;
	IBOutlet UIImageView *ball;
	IBOutlet UIImageView *box1;
	IBOutlet UIImageView *box2;
	float valueX;
	float valueY;
}
-(IBAction)done;
-(IBAction)playpinsound;
-(IBAction)start_game;
-(void)checkcollision;

@property (nonatomic, retain) UIImageView *ball;
@property (nonatomic, retain) UIImageView *box1;
@property (nonatomic, retain) UIImageView *box2;

@end

Second view main - SecondViewController.m
Code:
#import "SecondViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "ThirdViewController.h"
#import "TwoViewAppViewController.h"

@implementation SecondViewController

@synthesize ball;
@synthesize box1;
@synthesize box2;

-(void)checkcollision{
	if (CGRectIntersectsRect(box1.frame, ball.frame)){
		CGPoint newCenter = CGPointMake(-50, -50);
		box1.center = newCenter;
		[box1 removeFromSuperview];
	}
       if (CGRectIntersectsRect(ball.frame, box2.frame)){
		CGPoint newCenter = CGPointMake(-50, -50);
		box2.center = newCenter;
		[box2 removeFromSuperview];
	}
}

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
	valueX = acceleration.x*30.0;
	valueY = acceleration.y*30.0;
	
	int newX = (int)(ball.center.x + valueX);
	if (newX > 320-BALL_RADIUS)
		newX = 320-BALL_RADIUS;
	if (newX < 0+BALL_RADIUS)
		newX = 0+BALL_RADIUS;
	int newY = (int)(ball.center.y - valueY);
	if (newY > 460-BALL_RADIUS)
		newY = 460-BALL_RADIUS;
	if (newY < 55+BALL_RADIUS)
		newY = 55+BALL_RADIUS;
	
	CGPoint newCenter = CGPointMake(newX, newY);
	ball.center = newCenter;
	[self checkcollision];
}	

- (void)viewDidLoad {
}

//click done button back to first view
- (IBAction)done {
    [self.parentViewController dismissModalViewControllerAnimated:YES];
}

//click start button to on the accelerometer for the ball
-(IBAction)start_game{
	[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/30.0];
	[[UIAccelerometer sharedAccelerometer] setDelegate:self]; 
	[super viewDidLoad];
}

- (void)viewDidUnload {
}

@end
 

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
Hi dejo,

In my INTERFACE BUILDER:

File's Owner > referencing outlet > SecondViewController

SecondViewController > referencing outlet > File's Owner

Need help, please :(
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Sorry newbie80, I don't think I can help you further with this. I'm not sure how to instantiate a fresh view controller when the original was done via IB. Hope you find your answer though.
 

newbie80

macrumors member
Original poster
Mar 2, 2011
51
0
Sorry newbie80, I don't think I can help you further with this. I'm not sure how to instantiate a fresh view controller when the original was done via IB. Hope you find your answer though.
Hi Dejo,

It's okay, I know you already did your best to help me.:) Thank you, really appreciate your help and other here. And after I discussed with my colleague, the only way to do is to remove everything on second view and re-assign their position again on the second view. Almost completed my apps already, you helped me a lot in progress and I've learnt something new from you. Thanks again. See ya, problem solved ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.