Cannot create a UIColor using colorFromPatternImage: - iphone

The following works without issue:
toolBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"window_bkd.png"]];
However, I have similar statements scattered throughout my code and wanted to clean it up using the following statements, which crash on executing the first statement:
UIColor *bkdColor = [[UIColor alloc] colorWithPatternImage:[UIImage imageNamed:#"window_bkd.png"]];
toolBar.backgroundColor = bkdColor;
[bkdColor release];
Console output from the crash:
[UIPlaceholderColor colorWithPatternImage:]: unrecognized selector sent to instance 0x5203c90
Thanks for your help, I'm sure this is a Homer Simpson "doh!" mistake.

You accidently placed an alloc call in your second version, so you are calling colorWithPatternImage on an instance, while it is a class method. Doh! :-)
This is how it's done correctly:
UIColor *bkdColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"window_bkd.png"]];

Related

How do you change font in UISearchBar for iOS5?

I'm trying to change the font for all of my UISearchBar objects using the new "appearance" proxy for iOS5 with something like:
[[UISearchBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIFont fontWithName:#"Trebuchet MS" size:0.0], UITextAttributeFont,
nil]];
Every time I run this, I'm getting this error:
"2012-05-28 03:01:52.264 DirectDx_ClientApp[30039:15503] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIAppearance setTitleTextAttributes:]: unrecognized selector sent to instance 0x8460cf0'
* First throw call stack:
(0x1ea8022 0x3a4fcd6 0x1ea9cbd 0x1e0eed0 0x1e0ecb2 0x5e695 0x5dd78 0x217aa 0x789386 0x78a274 0x799183 0x799c38 0x78d634 0x203bef5 0x1e7c195 0x1de0ff2 0x1ddf8da 0x1dded84 0x1ddec9b 0x789c65 0x78b626 0x2164d 0x2895 0x1)
terminate called throwing an exception"
The method above works perfectly well with UITabBar and UINavigationBar.
Any insights?
Thanks very much in advance.
Try this,It's Working Fine for iOS 5.0 and up (iOS 7 also):
- (void)viewDidLoad
{
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:#"Helvetica" size:20]];
}
For iOS 8
- (void)viewDidLoad
{
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:#{
NSFontAttributeName: [UIFont fontWithName:#"Helvetica" size:20],
}];
}
Checkout the apple reference guide for uisearchbar, under customizing appearance you can see what methods are avialable to you for a uisearchbar. SetTitleAttributes is not possible for a uisearchbar and I can not see another method where you change the font of the uisearchbar by means of appearance.
Trying to call a method that is not supported by an object, will always give you an error. In your case setTitleArtributes is supported by other classes but sadly enough not for the uisearchbar.
There is a workaround.
Try using this code
UITextField *textField = [self.searchBar valueForKey: #"_searchField"];
[textField setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:17.0]];
Hope this will work for u.

Escaping the #" " characters in Objective C

I'm trying to set the background image of a view based on a passed "artistID." Here is my code so far:
NSString *backgroundImageName = [[NSString alloc]initWithFormat:#"artistbackground%i.png",_artistID];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:backgroundImageName]];
NSLog(#"%#",backgroundImageName);
unfortunately, for the parameter in ImageNamed, I'm passing in:
artistibackground1
instead of:
#"artistbackgound1"
Any ideas on how to escape the # and the quotes??
Thanks!!
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[NSString stringWithFormat:#"#%#",backgroundImageName]];
Essentially make two strings, it will add the #"" in the second.
You should use \ before the character you want. An example:
NSLog(#"\#\"Try\"");
The code prints
#"Try"
Don't forget that even string constants are NSString objects in Objective-C. This is part of the magic! I frequently see programmers new to the language writing this line:
[NSString stringWithFormat:#"#%#",backgroundImageName];
But you can simplify this line to:
[#"#" stringByAppendingString:backgroundImageName];
Magic!

iOS setSelectedImageTintColor uicolor not accepted

Following method is accepted if I set UIColor like:
[tabBarController.tabBar setSelectedImageTintColor:[UIColor greenColor]];
However I would like to call it like:
[tabBarController.tabBar setSelectedImageTintColor:colors];
when
- (UIColor *)colors{
UIColor *colorIcon = [UIColor greenColor];
return colorIcon;
}
and program returns an error "undeclared identifier colors". What am I doing wrong? Thanks
Try this instead:
[tabBarController.tabBar setSelectedImageTintColor:[self colors]];

I tried to separate alloc and initWithRed... messages, but it doesn't seems to work

Sorry if it is asked somewhere, but as a beginner, I need a very specific answer for my question. Where is wrong, correction and suggestions.
I write those under application didFinishLaunchingWithOption:
UIColor *myBackgroundColor = [[UIColor alloc]initWithRed:.87 green:.77 blue:.56 alpha:.99];
[window setBackgroundColor:myBackgroundColor];
It worked, and change the color of the background, and then I try to separate those two messages.
UIColor *myBackgroundColor = [UIColor alloc];
[myBackgroundColor initWithRed:.87 green:.77 blue:.56 alpha:.99]
[window setBackgroundColor:myBackgroundColor];
How should I code to make it run correctly? I will need both reason and corrections. Thanks a lot.
You can't assume that alloc and init has the same return value.
The following should work:
UIColor *myBackgroundColor = [UIColor alloc];
myBackgroundColor = [myBackgroundColor initWithRed:.87 green:.77 blue:.56 alpha:.99]
[window setBackgroundColor:myBackgroundColor];
I don't understand why you'd want to add the extra line though.
Use...
[UIColor colorWithRed:0.87 green:0.77 blue:0.56 alpha:0.99];

Application crashes on adding custom banner or label in adwhirl

What steps will reproduce the problem?
1.√ [adWhirlView replaceBannerViewWith:replacement];
What is the expected output? What do you see instead?
Exec Bad Access on replaceBanner with UILabel as specfied in example, it also crashes when I add my custom banner View
What version of the product are you using? On what operating system?
Mac OX 10.6.3 and AdWhirlSDK_iPhone_2.6.2.zip
Please provide any additional information below.
Whenever I integrate my custom add in Adwhirl so my application crashes [adWhirlView replaceBannerViewWith:replacement]; please let me know how to overcome this issue.
- (void)performEvent:(AdWhirlView *)adWhirlView {
// replace banner content
UILabel *replacement = [[UILabel alloc] initWithFrame:kAdWhirlViewDefaultFrame];
replacement.backgroundColor = [UIColor blackColor];
replacement.textColor = [UIColor whiteColor];
replacement.textAlignment = UITextAlignmentCenter;
replacement.text = [NSString stringWithFormat:#"Event performed, view %x", adWhirlView];
[adWhirlView replaceBannerViewWith:replacement];
[replacement release];
}
http://code.google.com/p/adwhirl/wiki/CustomEvents
EXC_BAD_ACCESS it represents that you have removed some objects and after that you trying to access it. OR you have created an autorelease objects and you are accessing the object afterwards. In both the cases the EXC_BAD_ACCESS is shown when you try to access an object which is not pointing to anything