Resolved xcode: how do I refresh a view?

Discussion in 'iOS Programming' started by newbie80, Mar 8, 2011.

  1. newbie80, Mar 8, 2011
    Last edited: Mar 18, 2011

    newbie80 macrumors member

    Joined:
    Mar 2, 2011
    #1
    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. :(
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    reloadData is for UITableViews, which u probably don't have.
    u need something like this
    [self.view setNeedsDisplay];
    or
    [self.view setNeedsLayout];
     
  3. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #3
    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]
    }
    
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4

    Is secondView a UIView or a UIViewController in this case? Did you get any warnings when you tried to Build that code?
     
  5. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #5
    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.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Again, I ask: Did you get any warnings when you tried to Build that code?
     
  7. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #7
    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
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    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.
     
  9. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #9
    Thanks, then do you have any idea what does the warning mean? :confused:
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    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?
     
  11. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    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.
     
  12. newbie80, Mar 9, 2011
    Last edited: Mar 9, 2011

    newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #12
    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'
     
  13. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #13
    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?
     
  14. newbie80, Mar 9, 2011
    Last edited: Mar 9, 2011

    newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #14
    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 :)
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    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.
     
  16. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #16
    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.
     
  17. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #17
    What YouTube tutorial? Being specific about where you are getting your help from helps us.

    And you're actually connecting it to a viewController in Interface Builder?


    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?
     
  18. newbie80, Mar 13, 2011
    Last edited: Mar 13, 2011

    newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #18
    I created as the tutorial from this YouTube link:
    http://www.youtube.com/watch?v=sCZhDOgVFNA

    From the connections there in InterfaceBuilder, I drag it to my viewController like what he did in video link above.


    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?
     
  19. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #19
    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
    
     
  20. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #20
    Hi dejo,

    In my INTERFACE BUILDER:

    File's Owner > referencing outlet > SecondViewController

    SecondViewController > referencing outlet > File's Owner

    Need help, please :(
     
  21. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #21
    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.
     
  22. newbie80 thread starter macrumors member

    Joined:
    Mar 2, 2011
    #22
    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 ;)
     

Share This Page