Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning iOS 5 Application Development
This is the forum to discuss the Wrox book Beginning iOS 5 Application Development by Wei-Meng Lee; ISBN: 978-1-1181-4425-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning iOS 5 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 10th, 2012, 02:27 PM
Registered User
Points: 40, Level: 1
Points: 40, Level: 1 Points: 40, Level: 1 Points: 40, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 3 - Dynamic Views

Hi all,
I followed the example for dynamic views on page 58 and run the script in an iOS simulator and its just a gray screen with nothing on it. I assume that since its a dynamically created scenario with a gray colored frame with button and an alert view, its just the gray frame that was the only successful dynamic element flushed out. The button was not there at all.

So i thought it could be a typo error on my end and downloaded the source code from this forum. It turns out the same thing. It could be Xcode is behaving funny or something is wrong with my complier.

Here's the downloaded code:
DynamicViewsViewController.h:
Code:
//
//  DynamicViewsViewController.h
//  DynamicViews
//
//  Created by Wei-Meng Lee on 30/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DynamicViewsViewController : UIViewController

@end
DynamicViewsViewController.m:
Code:
//
//  DynamicViewsViewController.m
//  DynamicViews
//
//  Created by Wei-Meng Lee on 30/7/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DynamicViewsViewController.h"

@implementation DynamicViewsViewController

- (void)loadView {
    //---create a UIView object---
    UIView *view =
    [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    
    //---set the background color to light gray---
    view.backgroundColor = [UIColor lightGrayColor];
    
    //---create a Label view---
    CGRect frame = CGRectMake(10, 15, 300, 20);
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont fontWithName:@"Verdana" size:20];
    label.text = @"This is a label";
    label.tag = 1000;
    
    //---create a Button view---
    //frame = CGRectMake(10, 70, 300, 50);
    frame = CGRectMake(10, 30, 300, 50);
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = frame;
    [button setTitle:@"Click Me, Please!" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor clearColor];
    button.tag = 2000;
    [button addTarget:self
               action:@selector(buttonClicked:)
     forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:label];
    [view addSubview:button];
    [view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
    
    for (int i=0; i<[view.subviews count]; ++i) {
        UIView *v = [view.subviews objectAtIndex:i];
        NSLog(@"%d", v.tag);
    }
    
    
    self.view = view;
    [view release];
    [label release];

}

-(void) printViews:(UIView *) view {
    if ([view.subviews count] > 0){
        for (int i=0; i<[view.subviews count]; ++i) {
            UIView *v = [view.subviews objectAtIndex:i];
            NSLog(@"View index: %d Tag: %d",i, v.tag);
            [self printViews:v];
        }
    } else
        return;
}

-(IBAction) buttonClicked: (id) sender{
    UIAlertView *alert = 
        [[UIAlertView alloc] initWithTitle:@"Action invoked!"
                                   message:@"Button clicked!"
                                  delegate:self
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
    [alert show];
    [alert release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [self printViews:self.view];
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
Thanx in advance...
Reply With Quote
  #2 (permalink)  
Old March 11th, 2012, 09:16 AM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

Having downloaded the project, the author's code presents the label and button, and brings up the alertView as intended, with a gray background. (The layout is not great, the commented out frame (frame = CGRectMake(10, 70, 300, 50);) is a better location for the UIButton than the one actually used.)

One thing that should be done differently, even though they are essentially the same thing, the method

Code:
-(IBAction) buttonClicked: (id) sender{
    UIAlertView *alert = 
    [[UIAlertView alloc] initWithTitle:@"Action invoked!"
                               message:@"Button clicked!"
                              delegate:self
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [alert show];
    [alert release];
}
should be

Code:
-(void) buttonClicked: (id) sender{
    UIAlertView *alert = 
    [[UIAlertView alloc] initWithTitle:@"Action invoked!"
                               message:@"Button clicked!"
                              delegate:self
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [alert show];
    [alert release];
}
Even though -IBAction evaluates to -void, its purpose to to indicate that an action method is set in the .xib file, not programatically.

This may be a dumb question, but you did build and run the author's project from Xcode? You aren't running the program directly in the simulator and possibly rerunning your program? (Your and the author's program do have identical names?) Try deleting the program from the simulator and reinstalling it. (Running a different version of a program with the same name does overwrite the previously installed version in the simulator, so this should happen automatically. But I am just speculating about the scenario you are getting)

If you can't resolve your issue send the project to
ipadhelper@me.com
and I will check it out.



Bob
Reply With Quote
  #3 (permalink)  
Old March 11th, 2012, 11:12 AM
Registered User
Points: 40, Level: 1
Points: 40, Level: 1 Points: 40, Level: 1 Points: 40, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

LoLxxx... of course its from Xcode... How can it be done otherwise? Is there otherwise?

Anyway, can i safely assume that Xcode has a caching system? I make it a point to repeatedly doing the same exercise until i can memorize the steps without looking at the book. Once i can do that, i add my own comments to 'explain to myself what each line does' in the code itself. But before i repeat each and every exercise, i would close Xcode, delete the folder to trash that was created using finder and empty out the trash. Then i would relaunch Xcode.

Is there an in-house cache in Xcode that still keeps the deleted projects?

Last edited by teamstar; March 11th, 2012 at 11:14 AM.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 5 Using Image Views... kenfordboise BOOK: Beginning Android Application Development 19 February 28th, 2012 08:27 AM
Chapter 4 - Switching Views tfiPadn00b BOOK: Beginning iPad Application Development 5 December 20th, 2010 10:01 AM
Chapter 4 - Two Views, not working for me... bigtalk BOOK: Beginning Ruby on Rails 1 January 3rd, 2008 06:30 AM
Chapter 7 - List Views Not Refreshing VBNovice BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 3 January 31st, 2007 07:26 PM
converting Access 2000 views to Sql views matta Classic ASP Professional 1 January 26th, 2005 02:37 PM



All times are GMT -4. The time now is 07:27 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.