Objective C UIActionSheet custom button color - objective-c

I'm trying to change the color of the button in my UIActionSheet. The problem (a really weird problem) is that if the orientation of the device is Landscape the button color doesn't change!
Some screenshot:
I can't figure out why!
Here's the code:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
[actionSheet.subviews enumerateObjectsUsingBlock:^(id _currentView, NSUInteger idx, BOOL *stop) {
if ([_currentView isKindOfClass:[UIButton class]]) {
[((UIButton *)_currentView).titleLabel setTextColor:[[MSAppDelegate sharedInstance] defaultColor]];
}
}];
/*
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
[button setTitleColor:[[MSAppDelegate sharedInstance] defaultColor] forState:UIControlStateNormal];
[button setTitleColor:[[MSAppDelegate sharedInstance] defaultColor] forState:UIControlStateSelected];
}
if ([subview isKindOfClass:[UILabel class]]) {
UILabel *label = (UILabel *)subview;
label.textColor = [[MSAppDelegate sharedInstance] defaultColor];
}
}
*/
}
I also tried the commented code, both work in Portrait but not in Landscape!
By the way either in Landscape and in Portrait the code is executed!
If you want to see all the code here's the link
Thank you in advance!

You could Subclass the UIActionSheet class and implement the tableView:willDisplayCell:forRowAtIndexPath: selector:
#interface MSActionSheet : UIActionSheet
#end
#implementation MSActionSheet
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[[cell subviews] enumerateObjectsUsingBlock:^(id v, NSUInteger idx, BOOL *stop) {
[[v subviews] enumerateObjectsUsingBlock:^(id v2, NSUInteger idx, BOOL *stop) {
if ([v2 isKindOfClass:[UILabel class]]) {
[(UILabel *)v2 setTextColor:[[MSAppDelegate sharedInstance] defaultColor]];
}
}];
}];
}
#end

use this method to set button title color then i am sure your problem will solve
[[[actionSheet valueForKey:#"_buttons"] objectAtIndex:0] setTitleColor:[[MSAppDelegate sharedInstance] defaultColor] forState:UIControlStateNormal];

Related

In collectionview Why the button tag give zero when i reload the uicollectionview objective c

At the very first time when collectionview load it show tag value perfect but when i reload uicollection then the value change and some value are zero why i can't understand please help me.
-(void)CheckUncheckFunctionality:(id)sender event:(id)event
{
UIButton *btn = (UIButton *)sender;
NSInteger subproductindex = btn.tag;
NSLog(#"Sub:%ld",(long)subproductindex);
if ([[arr objectAtIndex:subproductindex] isEqualToString:#"1"])
{
[btn setBackgroundImage:[UIImage imageNamed:#"unchacked.png"] forState:UIControlStateNormal];
[arr replaceObjectAtIndex:subproductindex withObject:#"0"];
}
else
{
[btn setBackgroundImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
[arr replaceObjectAtIndex:subproductindex withObject:#"1"];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CGCell *cell = [_CGCol dequeueReusableCellWithReuseIdentifier:#"CELL" forIndexPath:indexPath];
BOOL isExist;
isExist = [[arr objectAtIndex:indexPath.row] isEqualToString:#"1"];
cell.btn.tag = indexPath.row;
[cell.btn addTarget:self action:#selector(CheckUncheckFunctionality:event:) forControlEvents:UIControlEventTouchUpInside];
if (isExist) {
[cell.btn setBackgroundImage:[UIImage imageNamed:#"checked.png"]forState:UIControlStateNormal];
}
else{
[cell.btn setBackgroundImage:[UIImage imageNamed:#"unchacked.png"]
forState:UIControlStateNormal];
}
return cell;
}
- (void) searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {
searching = YES;
if([searchText length] > 0) {
[arrgroupMember removeAllObjects];
searching = YES;
[self searchCollectionView];
}
else{
searching = NO;
arrgroupMember = [DatabaseAccess GetDataForMember];
}
[_CGCol reloadData];
}
on searching when collectionview gets reload d tag is set to 0.How could i preserve that

UITableViewCell setSelected method not getting called

I am on iOS7 and have a UITableViewCell subclass for my UITableView with static cells. I am overriding the setSelected method in the implementation.
For some reason, the method only gets called when the table loads but doesn't get called when the cell is actually tapped and selected.
What am I doing wrong here? How do I get it to work?
#implementation StudentMenuMultipleOptionsTableViewCell
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
if (selected) {
UIView *view = [UIView new];
view.backgroundColor = [UIColor colorWithRed:0.542 green:0.788 blue:0.060 alpha:1.000];
self.selectedBackgroundView = view;
}
else {
for (UIView *view in self.subviews) {
if ([view isKindOfClass:[BlackBackgroundSelectedButton class]]) {
BlackBackgroundSelectedButton *button = (BlackBackgroundSelectedButton *)view;
button.selected = NO;
[button setWhite];
}
}
}
}
#end
The problem was that I was using the setSelected method. The method that needs to be used for the newer iOS versions is:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated;

How to find UIButton by title

I am having a UIView that has button in it. The buttons are not in some array.
I would like to look at the whole UIView (using for loop) and look for a button by its title, and later remove it.
- (void)removeButtonByTitle:(NSString*)name
{
for (buttons in view) {
// find the button with the name "name" and remove it from the view
}
}
I couldn't find a way to do that without saving their names/pointers to an array.
It's generally preferable to use the tag property for this. Then you can simply find the button with the viewWithTag: method and don't have to adjust your code if you decide to change the button title or localize your app.
If you really need to find a button by its title, you could do it like this:
NSString *buttonTitle = #"name";
UIButton *buttonWithTitle = nil;
for (UIButton *button in view.subviews) {
if (![button isKindOfClass:[UIButton class]]) continue;
if ([[button currentTitle] isEqualToString:buttonTitle]) {
buttonWithTitle = button;
break;
}
}
//do something with the button...
Well you could do it like this:
for (UIView *v in view.subviews)
if ([v isKindOfClass:[UIButton class]] && [[(UIButton *)v currentTitle] isEqualToString:#""])
//remove
But I must say that doesn't sound like a robust solution, your button title could change during localization or it may be different for different states.
Do that:
- (void)removeButtonByTitle:(NSString *)name
{
for (UIView *tempView in self.subviews)
{
if ([tempView isKindOfClass:[UIButton class]]) // make sure it's actually a UIButton
{
UIButton *button = tempView;
if ([button.titleLabel.text isEqualToString:name]) // compare the title
{
[button removeFromSuperview];
}
}
}
}
Check this:
-(void)removeButtonWithTitle:(NSString*)titleString {
NSArray *subViews = [self.view subviews];
[subViews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if([obj isKindOfClass:[UIView class]]) {
NSArray *subViewsArray = [(UIView*)obj subviews];
for (int i=0; i<[subViewsArray count]; i++) {
id subViewObj = [subViewsArray objectAtIndex:i];
if([subViewObj isKindOfClass:[UIButton class]] && [[(UIButton*)subViewObj titleLabel].text isEqualToString:titleString]) {
[subViewObj removeFromSuperview];
*stop = YES;
break;
}
}
}
}];
}

NSArray remove last object of type?

Working with an array of UIViews and UIImageViews ([[[UIApplication sharedApplication] window] subviews]). I need to remove only the object of the highest index of the UIImageView type.
You can use indexOfObjectWithOptions:passingTest: method to search the array in reverse for an object that passes a test using a block, and then delete the object at the resulting position:
NSUInteger pos = [myArray indexOfObjectWithOptions:NSEnumerationReverse
passingTest:^(id obj, NSUInteger idx, BOOL *stop) {
return [obj isKindOfClass:[UIImageView class]]; // <<== EDIT (Thanks, Nick Lockwood!)
}];
if (pos != NSNotFound) {
[myArray removeObjectAtIndex:pos];
}
another block-based solution
[window.subviews enumerateObjectsWithOptions:NSEnumerationReverse
usingBlock:^(id view, NSUInteger idx, BOOL *stop)
{
if ([view isKindOfClass:[UIImageView class]]){
[view removeFromSuperview];
*stop=YES;
}
}];
non-block solution:
for (UIView *view in [window.subview reverseObjectEnumerator])
{
if ([view isKindOfClass:[UIImageView class]]){
[view removeFromSuperview];
break;
}
}
I published some demo code, that shows both solutions.
How about:
UIWindow *window = [[UIApplication sharedApplication] window];
UIView *imageView = nil;
for (UIView *view in window.subviews)
{
if ([view isKindOfClass:[UIImageView class]])
{
imageView = view;
}
}
//this will be the last imageView we found
[imageView removeFromSuperview];

Keep UIAlertView displayed

I have a UIAlertView with a textField on it and two buttons: Save & Cancel. When the Save button is tapped I am checking if the text field isn't empty and after if it is I simply want to change the textFields placeholder to: #"enter a name please" and KEEP the alert view on screen. However it is automatically dismissed.
How do I override that?
Add a target to the textfield in a subclassed alertView. You can subclass the alertView and not dismiss as described in this post
[[alertView textFieldAtIndex:0] addTarget:self action:#selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
Then write a function called textFieldDidChange that checks the current textfield of your alertView and set a boolean value so you know whether or not to dismiss the alert.
- (void) textFieldDidChange
{
NSString *alertViewText = [[alertView textFieldAtIndex:0] text];
if ([alertViewText isEqualToString:#""]) {
[alertView setMessage:#"Enter a name please."];
} else {
[alertView setMessage:#"Default Message"];
}
}
* Alternatively, I would suggest disabling "Save" when it is empty and not have to subclass. *
- (void) textFieldDidChange
{
NSString *alertViewText = [[alertView textFieldAtIndex:0] text];
if ([alertViewText isEqualToString:#""]) {
[alertView setMessage:#"Enter a name please."];
for (UIViewController *view in alertView.subview) {
if ([view isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)view;
if ([[[button titleLabel] text] isEqualToString:#"Save"])
[button setEnabled:NO];
}
}
} else {
[alertView setMessage:#"Default Message"];
for (UIViewController *view in alertView.subview) {
if ([view isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)view;
if ([[[button titleLabel] text] isEqualToString:#"Save"])
[button setEnabled:YES];
}
}
}
}