How To Use Blocks in iOS 5 Tutorial - Part 2

Discussion of the official tutorials published on raywenderlich.com. Please only discuss the official tutorials here - for general questions, use the General Discussion forum instead.

How To Use Blocks in iOS 5 Tutorial - Part 2

Postby rwenderlich » Fri Feb 10, 2012 11:00 am

This is the official thread to discuss the following blog post: How To Use Blocks in iOS 5 Tutorial - Part 2
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Team Member
Site Admin
 
Posts: 2237
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 372 times

Re: How To Use Blocks in iOS 5 Tutorial Part 2

Postby ksteelman » Fri Feb 10, 2012 7:30 pm

Hey Adam,

Here is my question I mentioned on twitter.

If the parent view is a viewcontroller that has a child of uitableview and uitableviewcell. we'll call the first uitableviewcell the 1st cell. In 1st cell i have a uitableview and 1st cell also has a child of a uitableview delegate/data source with a 2nd cell. Right not I implement a delegate on 1st cell and when the method -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath gets called i pass which cell was selected through the 1st cell delegate method to the view controller to handle any UI changes. Anyway to handle this behavior with blocks? I've been trying to figure that out and implement more blocks through my app and these tutorials have been a huge help!

Kyle
Twitter: @KyleSteelman
ksteelman
Uber Haxx0r
 
Posts: 52
Joined: Mon Jan 17, 2011 10:05 pm
Has thanked: 2 times
Been thanked: 1 time

Re: How To Use Blocks in iOS 5 Tutorial Part 2

Postby zeiteisen » Fri Feb 10, 2012 10:10 pm

Hello Adam,

thank you for this tutorial.

I got two questions:
Who is the receiver of return in this call?
// Calling that method with a block
- (IBAction)buttonTapped:(id)sender {
[self doMathWithBlock:^(int a, int b) {
return a + b;
}];
}

I'm using ARC and get cyclic retains if I use self.method in a block.
zeiteisen
Uber Haxx0r
 
Posts: 79
Joined: Mon May 02, 2011 8:41 pm
Has thanked: 5 times
Been thanked: 4 times

Re: How To Use Blocks in iOS 5 Tutorial Part 2

Postby Elliott » Fri Feb 10, 2012 11:48 pm

Excellant tutorial - many thanks!
User avatar
Elliott
Uber Haxx0r
 
Posts: 454
Joined: Sun Jan 02, 2011 10:34 pm
Location: UK
Has thanked: 4 times
Been thanked: 112 times

Re: How To Use Blocks in iOS 5 Tutorial Part 2

Postby acburk » Sat Feb 11, 2012 8:49 am

mrpills wrote:Hey Adam,

Here is my question I mentioned on twitter.

If the parent view is a viewcontroller that has a child of uitableview and uitableviewcell. we'll call the first uitableviewcell the 1st cell. In 1st cell i have a uitableview and 1st cell also has a child of a uitableview delegate/data source with a 2nd cell. Right not I implement a delegate on 1st cell and when the method -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath gets called i pass which cell was selected through the 1st cell delegate method to the view controller to handle any UI changes. Anyway to handle this behavior with blocks? I've been trying to figure that out and implement more blocks through my app and these tutorials have been a huge help!

Kyle



hmm, I'm still having trouble understanding the relationship.

we'll call the first uitableviewcell the 1st cell. In 1st cell i have a uitableview


Are you saying you have a UITableView inside of a UITableViewCell?

Can you post the code or do you have a sample project you could send?
acburk
Team Member
Uber Haxx0r
 
Posts: 492
Joined: Tue Jul 19, 2011 5:25 pm
Has thanked: 3 times
Been thanked: 55 times

Re: How To Use Blocks in iOS 5 Tutorial Part 2

Postby acburk » Sat Feb 11, 2012 9:12 am

zeiteisen wrote:Hello Adam,

thank you for this tutorial.

I got two questions:
Who is the receiver of return in this call?
// Calling that method with a block
- (IBAction)buttonTapped:(id)sender {
[self doMathWithBlock:^(int a, int b) {
return a + b;
}];
}

I'm using ARC and get cyclic retains if I use self.method in a block.


Hi,

First Question:
The receiver of the return inside the block depends on the implementation of doMathWithBlock. For example if doMathWithBlock was defined as this:

Code: Select all
- (void)doMathWithBlock:(BOOL(^)(int a, int b))mathBlock {
     someVariable = mathBlock(variable1, variable2);
}


So it really depends on how the doMathWithBlock function decides to use that block.

Second Question:
I'm afraid I don't quite understand. I don't see why using (do you mean setting or getting?) the property inside of a block would mess with the retain cycle though. Can you provide more detail? If you are talking about using the 'dot syntax' to get or set a property, you do have to watch out for retain cycles. Look into creating the properties as weak references to help resolve retain cycles with properties and ARC.
acburk
Team Member
Uber Haxx0r
 
Posts: 492
Joined: Tue Jul 19, 2011 5:25 pm
Has thanked: 3 times
Been thanked: 55 times

Re: How To Use Blocks in iOS 5 Tutorial Part 2

Postby gyroanto » Sat Feb 11, 2012 1:28 pm

Thank you Adam I've completed the tutorial, but my question is:
I'm new in Xcode and agree understand all is possible. For example I've a website and I can host a MySQL+PHP database for the foods, but not understand well the relationship with JSON and files of your tutorial.
Can you explain?
gyroanto
Baby Hacker
 
Posts: 5
Joined: Sun Jan 22, 2012 3:37 pm
Has thanked: 0 time
Been thanked: 0 time

Re: How To Use Blocks in iOS 5 Tutorial Part 2

Postby ksteelman » Sun Feb 12, 2012 1:42 am

Yeah thats exactly what I have. I am trying to detect touches in the pulse tutorial app http://www.raywenderlich.com/4723/how-to-make-an-interface-with-horizontal-tables-like-the-pulse-news-app-part-2 and right now i've just built delegates for the horizontal cell so when a cell is selected from within the tableview in horizontaltableviewcell to pass info back to the view controller

acburk wrote:
mrpills wrote:Hey Adam,

Here is my question I mentioned on twitter.

If the parent view is a viewcontroller that has a child of uitableview and uitableviewcell. we'll call the first uitableviewcell the 1st cell. In 1st cell i have a uitableview and 1st cell also has a child of a uitableview delegate/data source with a 2nd cell. Right not I implement a delegate on 1st cell and when the method -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath gets called i pass which cell was selected through the 1st cell delegate method to the view controller to handle any UI changes. Anyway to handle this behavior with blocks? I've been trying to figure that out and implement more blocks through my app and these tutorials have been a huge help!

Kyle



hmm, I'm still having trouble understanding the relationship.

we'll call the first uitableviewcell the 1st cell. In 1st cell i have a uitableview


Are you saying you have a UITableView inside of a UITableViewCell?

Can you post the code or do you have a sample project you could send?
Twitter: @KyleSteelman
ksteelman
Uber Haxx0r
 
Posts: 52
Joined: Mon Jan 17, 2011 10:05 pm
Has thanked: 2 times
Been thanked: 1 time

Re: How To Use Blocks in iOS 5 Tutorial Part 2

Postby acburk » Sun Feb 12, 2012 6:38 am

JSON is the format that we have chosen to return the data back from the web service to the app, which know how to decode JSON and convert the properties so we can use them.

gyroanto wrote:Thank you Adam I've completed the tutorial, but my question is:
I'm new in Xcode and agree understand all is possible. For example I've a website and I can host a MySQL+PHP database for the foods, but not understand well the relationship with JSON and files of your tutorial.
Can you explain?
acburk
Team Member
Uber Haxx0r
 
Posts: 492
Joined: Tue Jul 19, 2011 5:25 pm
Has thanked: 3 times
Been thanked: 55 times

Re: How To Use Blocks in iOS 5 Tutorial Part 2

Postby fulkron » Sun Feb 12, 2012 7:36 am

Thank you Adam.
Interesting Tutorial.
Dario
fulkron
n00b
 
Posts: 2
Joined: Mon Dec 19, 2011 6:26 am
Has thanked: 0 time
Been thanked: 0 time

Next

Return to Official Tutorials

Who is online

Users browsing this forum: No registered users and 8 guests