Tuesday, October 18, 2011

Matt Gallagher: Variable argument lists in Cocoa

Itt van, de ami nekem kellett azt bemasolom:

The va_list, va_start, va_arg and va_end are all standard C syntax for handling variable arguments. To describe them simply:

  • va_list - A pointer to a list of variable arguments.
  • va_start - Initializes a va_list to point to the first argument after the argument specified.
  • va_arg - Fetches the next argument out of the list. You must specify the type of the argument (so that va_arg knows how many bytes to extract).
  • va_end - Releases any memory held by the va_list data structure.

Generally speaking, you can use this for loop for any variable argument situation where your arguments are all the same type. Other cases are a bit trickier but far less common — I'm sure you can work out how they would work if needed.

va_list in Cocoa

A number of classes in Cocoa have methods that take variable numbers of arguments. In most cases, these classes will also have an equivalent method that takes a va_list.

We can see an example of these va_list equivalents by looking at NSString. NSString declares the class method stringWithFormat:... (which takes a variable number of arguments) andNSString also declares the instance method initWithFormat:arguments: (where the argumentsparameter is a va_list) which handles the equivalent behavior of stringWithFormat:....

These va_list methods are used in the situation where your class defines a method with a variable argument list and you need to pass those variable arguments into the Cocoa method. For example, if the StringContainer class listed above declared the method:

- (void)setContentsWithFormat:(NSString *)formatString, ...;

The implementation of this method would be as follows:

- (void)setContentsWithFormat:(NSString *)formatString, ...
{
[contents autorelease];
va_list args;
va_start(args, formatString);
contents = [[NSString alloc] initWithFormat:formatString arguments:args];
va_end(args);
}

The va_list parameter allows us to pass our own variable argument list to the Cocoa method so that the Cocoa method can handle the arguments.

Friday, September 2, 2011

willShowViewController

Ahhoz, hogy mukodjon egyreszt a header file-ba be kell venni a UINavigationControllerDelegate-et is, masreszt fontos, hogy a NavigationController delegate-jet beallitsuk. En ezt az utobbit felejtettem le es nem ertettem, hogy miert nem hivodik meg a navigationController:willShowViewController.

Tuesday, August 16, 2011

UIButton(ok) elhelyezese UITableView aljan

Itt van 2 pelda. Az elsoben csak 1 UIButton-t teszek ki, a masodikban 3-at. Az elsoben csak a UITableView utolso sectionjenek footer-jebe teszem ki a gombokat. A masodikbol ezt kihagytam. Amikor csak 1 gomb van akkor eleg az AutoResizingMask-ot a Width-re allitani. 3 gomb eseten mar mindegyiket mashogy kell beallitani. Igy ha elforgatjuk a device-t (es persze a shouldAutorotateToInterfaceOrientation YES-t ad vissza), akkor megfeleloen lesznek atmeretezve a gombok.

1 gombosnal hibatlan, de 3 gombosnal az a kis hibaja van, hogy landscape mode-ban kicsit nagyobb lesz a tavolsag a gombok kozott mint portrait mode-ban.

Tuesday, July 12, 2011

nev-ertek megjelenitese UITableViewCell-ben

Kellett egy olyan UITableViewCell amiben nev-ertek parokat mutatok. Valamiert custom UITableViewCell-t csinaltam erre amibe tettem 2 UILabel-t ahelyett, hogy siman UITableViewCellStyleValue2-vel hoztam volna letre a cellakat. Most kijavitottam, de van par dolog amire itt is oda kell figyelni:

Az egyik dolog, hogy mivel a detailTextLabel-be esetleg tobb sornyi szoveg is kerulhet amit nem akarok trunc-olni ezert a cella magassagat be kell allitani (nem allitja magatol a device) valahogy igy:

- (CGFloat)tableView:(UITableView *)tableView_ heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat kMinCellHeight = 44.0;
NSString *text = ZXGetPropertyValueForRow([indexPath row]);
CGSize textSize = CGSizeMake(tableView_.frame.size.width - 120.0, FLT_MAX);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:15.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 25.0f;
return MAX(size.height, kMinCellHeight);
}

Azert hasznalom tableView.frame.size.width-120.0-at es nem a tobb helyen ajanlott 200.0f koruli ertekek valamelyiket, mert ez landscape modban is jo lesz. Ha a cella accessoryType-ja nem UITableViewCellAccessoryTypeNone hanem UITableViewCellAccessoryTypeDisclosureIndicator akkor inkabb -140.0-et kell hasznalni, mert a disclosureIndicator (>) elvesz 20 pixelt a detailTextView-tol. Ezek a 120.0-140.0-es ertekek csak hozzavetoleg jok. Azt meg nem sikerult kideriteni, hogy peldaul a Contacts App-ban ha felveszek egy tobb soros notes-ot akkor hogyan csinalja meg pontosan a cella magassagat. (Pedig ez lenne a lenyeg.)
A 25.0f-es ertek pedig ugy jott ki, hogy megneztem milyen magas a cella, ha nem varialok semmit es sorok_szama*19.0+25.0 jott ki azaz az egy soros ertekeknel 44.0f. Kivetel ez alol a Group-ok elso es utolso sora, ahol 45.0f-es a default magassag. (Csak Grouped Style-os UITableView-nal neztem ezeket az ertekeket.) Az elso es utolso sorok magassagat meg hiaba is allitottam 44.0-re, az minimum 45.0 lett.
A 15.0f-es meretet pedig a systemFontOfSize-nal pedig tobb helyen olvastam es probalgatassal nekem is ez jott ki.
Ahhoz persze, hogy tobb soros legyen a detailTextLabel meg be kellett allitani a numberOfLines-t 0-ra.


Egy masik problema volt, hogy a textLabel-be pont nem fert bele amit bele akartam irni. Eloszor azzal probalkoztam, hogy textLabel-en beallitom a betumeretet a setFont-tal ([[cell textLabel] setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];), de nem akartam azokat megvaltoztatni amik a default font-tal is elfernenek.
Egy masik megoldas, hogy textLabel-re is beallitjuk a numberOfLines-t mondjuk 0-ra, de mivel a textLabel es a detailTextLabel betumerete nem ugyanakkora ezert ha mindketto 2 soros akkor a masodik soruk el van csuszva egymastol ami eleg benan nez ki.
Vegul azt valasztottam (ami nem tul hosszu szovegeknel mukodik), hogy a numberOfLines-t hagytam a default 1-en (ez fontos!) es beallitottam a minimumFontSize-ot (ami a textLabel-nel kb 12) 10-re. Igy ami elozoleg pont nem fert bele azt meg beleferthet es nem is lesz tul csunya. Ehhez be kell allitani az adjustFontSizeToFitWidth-et is true-ra.
[[cell textLabel] setAdjustsFontSizeToFitWidth:true];
[[cell textLabel] setMinimumFontSize:10.0];

Monday, July 11, 2011

Assertion kikapcsolasa release-nel

Innen.

I typically use assertions during development only – you can disable assertions by adding the preprocessor macro NS_BLOCK_ASSERTIONS in the build settings for your Release builds.

The screenshot below shows how to configure the Build Settings inside Xcode 4 to add NS_BLOCK_ASSERTIONS.

Wednesday, July 6, 2011

Codesign error lejart provisioning profile update-je utan

Lejart a provisioning profile amit renew-oltam, letoltottem, de futtatasnal codesign error jott.
Itt talaltam ezt a megoldast, ami mukodott:

- select don't code sign from Project > Build Settings > Code Signing
- Clean the build
- Build it again
- should show an error like "this stuff needs signing"
- Choose the new profile from the Code Signing
- Rebuild