Wrox Programmer Forums
|
BOOK: Beginning iOS 4 Application Development
This is the forum to discuss the Wrox book Beginning iOS 4 Application Development by Wei-Meng Lee; ISBN: 978-0-470-91802-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning iOS 4 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 16th, 2011, 09:19 PM
Registered User
 
Join Date: May 2011
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Cool Chapter 3 - Page 69

Hi All,

On page 69, towards the bottom, there's code for a method called "printViews":
Code:
-(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;
}
etc.

Well just WHERE is this method ever called from within the program? I'm not seeing its output in XCode's Console - or anywhere else for that matter.
Should it be visible anywhere?

Anyone have any ideas how to call it, where to call it from, how to get it to do its thing and display its NSLog output?

any help would be greatly appreciated!

Last edited by sirab33; May 16th, 2011 at 09:22 PM..
 
Old May 16th, 2011, 11:11 PM
Friend of Wrox
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

In the previous incarnation of this book this code was presented as a way to list the subviews. I don't have the current book, but the downloadable source code has -printViews commented out in the the -loadView method. You just need to place the call at a point after the subviews have been added (or maybe call before and again after they have been added, or at various points to track the changes in the array as you add and remove views). So various points in the -loadView method or in the -viewDidLoad method are possible places to call the method. An additional option would be to add a button to the interface and call it as an action method.

Bob
 
Old May 17th, 2011, 04:24 PM
Registered User
 
Join Date: May 2011
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok I tried it - and it works - sorta.

As per your suggestion, I did look at the downloadable source code and see that -printViews is indeed commented out in the -loadView method.
So, I copy-pasted it into my XCode project (and un-commented it of course) - and it runs successfully (meaning we now do see the NSLog outputs in Console) but I get a "may not respond to printViews" warning in Xcode - and I'm not sure why its doing that... any ideas?
 
Old May 18th, 2011, 12:40 AM
Friend of Wrox
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

You are getting the warning because the method is not declared in the header file. If you add it there the warning will go away. A second option is to place the -printViews method definition above any calls to it in the .m file. In 'C' based languages, if a method (or function) is not declared in the header file, but is defined in the implementation file, methods (or functions) following it in the implementation file will be aware of its existence, those preceding it will not.
e.g. assume the following methods have not been declared in the header, but only appear in the implementation:

-(void)methodA
{}
-(void)methodB
{}

methodB can call methodA without a warning, but methodA cannot call methodB without generating a warning.

In many languages this would not compile, but because Objective C supports dynamic binding, only a warning is generated. The program will run fine as long as the method being called is available, so methodA can successfully call methodB. If methodB did not exist, the program would crash at runtime.

Bob
 
Old May 19th, 2011, 11:01 AM
Registered User
 
Join Date: May 2011
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Smile Got it - but...

As per your suggestion, I moved the 'printViews' method to the top of the code - and it works with no errors or warnings.

It makes total sense - as it is very well explained - however... I distinctly recall from some of the exercises in the Kochan Objective C book that the order of the code was NOT important.
Now, it can't be an arbitrary thing where in some instances it is important and in others it isn't, so what are the rules there?
 
Old May 19th, 2011, 11:17 AM
Friend of Wrox
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

If the method name is in the header, then the method is always visible in the class implementation file. If the method is not in the header (i.e. a private method) it is only visible to methods below it without generating warnings. The order is irrelevant in these cases. If the method is not declared in the header, and is called by a method appearing above it in the implementation file a warning will be generated, but the method will be found and executed at runtime, so again, the order that you place your methods is irrelevant. If you wish to have private methods, and avoid generating warnings, simply create a category in your .m file declaring the method signatures.

I prefer to compile with no warnings.

Bob
 
Old May 19th, 2011, 11:36 AM
Registered User
 
Join Date: May 2011
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up got it :-)

yes, declaring everything in the Header file will most certainly prevent any potential issues for sure - I just thought I saw something different someplace else - but I guess just remembered wrong.

my bad.

so, many thanks, much obliged :-)
 
Old September 7th, 2011, 08:54 PM
Authorized User
 
Join Date: Feb 2011
Posts: 13
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Holy skit! I got it to work!
Thanks in large part to your discussion & a helluva lot of hacking since--
1. The book source I have doesn't have this in it at all, commented out or otherwise. Maybe I need to do a fresh download, I guess;
2. I really don't understand either declaring a method nor calling one in objC.
But I guess I got it!

Just getting back to the book after a forced hiatus of several months, so I restarted from the beginning. Funny though, I don't recall having this problem before.

Thanks yet again, Bob!
Chet





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 2 page 23 youngbones BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 March 30th, 2011 12:21 PM
WS processing in an example on page 69 lkuty BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 2 March 7th, 2011 05:24 AM
chapter 8, page 300 misken BOOK: Beginning SharePoint 2010 Development 3 September 13th, 2010 05:17 PM
chapter 2 - page 41 problem creating the page.. jorjiana BOOK: Beginning Oracle Application Express ISBN: 9780470388372 2 August 22nd, 2009 04:25 AM
Chapter 2, pages 68/69 moviesite.php ruairiglenn04 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 August 1st, 2004 05:27 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.