How can I gain access to a UIAlertView button's titleLabel property? I've tried the following code:
for (UIView *view in action.subviews)
{
if([view isKindOfClass:[NSClassFromString(#"UIAlertButton") class]])
{
//Do something with title label font
}
}
try like this ,you can change the view.tag based on your requirement
- (void)willPresentAlertView:(UIAlertView *)alertView{
for(UIView *view in alertView.subviews)
if(([view isKindOfClass:[UIButton class]]) && (view.tag ==1)){
UIButton *btn = (UIButton *)view;
btn.titleLabel.text=#"your title";
}
}
Related
How can I remove all buttons from a view using iOS 7?
Here's code that works in earlier versions of iOS:
for(UIView *view in cell.subviews){
if([view isMemberOfClass:[UIButton class]]){
[(UIButton *)view removeFromSuperview];
}
}
First you need to get all subviews from view and then check all view is type of UIButton.For more info see this...
for (UIView *view in self.view.subviews)
{
if ([view isMemberOfClass:[UIButton class]])
{
[(UIButton *)view removeFromSuperview];
}
}
It looks like you're simply not doing a loop through the subviews. Assuming this is a view controller:
NSArray * allSubviews = [self.view subviews];
for(UIView view in allSubviews)
{
if([view isMemberOfClass:[UIButton class]])
{
[view removeFromSuperview];
}
}
You also don't need to cast "view" to "UIButton *" here because the base class of "UIView" is what implements "removeFromSuperview".
Sorry for late reply. Actually this part of code will work until iOS 6.0, but in iOS 7.0 and onwards, the code is not working.
So finally I found the solution to remove all subview from UIScrollview with the following statement.
[scrollView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)].
Try, this will work
for(UIView *view in self.view.subviews)
{
if ([view isKindOfClass:[UIButton class]])
{
[view removeFromSuperview];
}
}
I would like to customize the delete button which is shown when performing the 'swipe to left'-action on a tableview cell. I currently set up a subclass of a UITableViewCell but also want to customize the delete-button which is being shown.
My goal is to place three buttons when swiping.
I choose for another implementation where I was using a UIScrollview in each cell.
http://www.teehanlax.com/blog/reproducing-the-ios-7-mail-apps-interface/
Accepted answer will not work on iOS 7, as there is now UITableViewCellContentView in between. So subviews loop now should look like this(if you want to support older iOS versions too, use currently accepted answer for iOS 6.1-)
for (UIView *subview in self.subviews) {
for (UIView *subview2 in subview.subviews) {
if ([NSStringFromClass([subview2 class]) rangeOfString:#"Delete"].location != NSNotFound) {
// Do whatever you want here
}
}
}
This might help you.
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
for (UIView *subview in self.subviews)
{
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationControl"])
{
UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
[deleteBtn setImage:[UIImage imageNamed:#"arrow_left_s11.png"]];
[[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
}
}
}
}
Referenced from:
Customize the delete button in UITableView
create custom delete button for uitableview
Custom Delete button On Editing in UITableView Cell
-(void)willTransitionToState:(UITableViewCellStateMask)state{
NSLog(#"EventTableCell willTransitionToState");
[super willTransitionToState:state];
if((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake( 320,0, 228, 66)];
[deleteBtn setImage:[UIImage imageNamed:#"BtnDeleteRow.png"]];
[[self.subviews objectAtIndex:0] addSubview:deleteBtn];
[self recurseAndReplaceSubViewIfDeleteConfirmationControl:self.subviews];
[self performSelector:#selector(recurseAndReplaceSubViewIfDeleteConfirmationControl:) withObject:self.subviews afterDelay:0];
}
}
The solutions above didn't work for me for iOS 7, at - (void)willTransitionToState:, the delete button wasn't in the view heirarchy so I wasn't able to manipulate anything. I ended up doing everything on - (void)didTransitionToState:. The example below was specifically for when my cells had some spacing at the top so I'm altering the frame of the delete button. If you want to customize the delete button, you can just add a view on top of the delete button or replace it with your own UIButton
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//your own stuff
//for some reason, editingAccessoryView cannot be nil and must have a non-CGRectZero frame
self.editingAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
}
return self;
}
- (void)didTransitionToState:(UITableViewCellStateMask)state
{
[super didTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
UIView *deleteButton = [self deleteButtonSubview:self];
if (deleteButton) {
CGRect frame = deleteButton.frame;
frame.origin.y += defined_padding;
frame.size.height -= defined_padding;
deleteButton.frame = frame;
}
}
}
- (UIView *)deleteButtonSubview:(UIView *)view
{
if ([NSStringFromClass([view class]) rangeOfString:#"Delete"].location != NSNotFound) {
return view;
}
for (UIView *subview in view.subviews) {
UIView *deleteButton = [self deleteButtonSubview:subview];
if (deleteButton) {
return deleteButton;
}
}
return nil;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(#"will rotation");
for (UIButton *button in self.view.subviews) {
[button removeFromSuperview];
}
}
I have a problem with this code. I need to remove only UIButtons from my view. But this code also remove all subviews of my self.view. How can I solve this?
Do this:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(#"will rotation");
for (id subview in self.view.subviews) {
if([subview isKindOfClass:[UIButton class]]) //remove only buttons
{
[subview removeFromSuperview];
}
}
}
You are iterating all the views and casting them to UIButton, not iterating UIButtons.
Try this:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSLog(#"will rotation");
for (UIView *button in self.view.subviews) {
if ([button isKindOfClass:[UIButton class]]) {
[button removeFromSuperview];
}
}
}
for (id button in self.view.subviews)
{
if(button isKindOfClass:[UIButton class])
{
[button removeFromSuperview];
}
}
self.view.subviews will fetch all subviews, and using the type UIButton * in that way does not filter the list, it just hints to the compiler that you expect to be able to treat the objects like UIButtons. You must inspect each one, like this:
for (UIView *subview in self.view.subviews) {
if([subview isKinfOfClass:[UIButton class]])
[subview removeFromSuperview];
}
One option is to create a tag at the top of the file
#define BUTTONSTODELETE 123
and then set each of the buttons.tag = BUTTONSTODELETE
Finally :
for (UIButton *v in [self.view subviews]){
if (v.tag == BUTTONSTODELETE) {
[v removeFromSuperview];
Guarantees only items with that tag will be removed
EDIT: Maulik and fbernardo have a better, less messy way
I have been using the following code to loop through specific classes in my subviews.
for (int i = 0; i < [[self.view subviews] count]; i++) {
if ([[self.view.subviews objectAtIndex:i] class] == [UIButton class]) {
}
}
But i feel like there should be a better way. Could someone please help me out?
Thanks.
for(UIView *v in [self.view subviews]) {
if ([v isKindOfClass:[UIButton class]]) {
...
}
}
You can also try this to iterate through a specific class in a view. I find it a bit cleaner.
for (UIButton *button in [self.view subviews]) {
// do whatever e.g. button.layer.cornerRadius = 11.0;
}
Use this instead:
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UIButton class]]) {
//...
}
}
Alternately, you could use a block by sending the subviews array -enumerateObjectsUsingBlock:.
- (void)webViewDidFinishLoad:(UIWebView *)YouTubePlayer {
UIButton *b = [self findButtonInView:YouTubePlayer];
[b sendActionsForControlEvents:UIControlEventTouchUpInside];
}
- (UIButton *)findButtonInView:(UIView *)view {
UIButton *button = nil;
if ([view isMemberOfClass:[UIButton class]]) {
return (UIButton *)view;
}
if (view.subviews && [view.subviews count] > 0) {
for (UIView *subview in view.subviews) {
button = [self findButtonInView:subview];
if (button) return button;
}
}
return button;
}
This code is not working, i am getting 2 warnings on the line UIButton *b = [self findButtonInView:YouTubePlayer]; the warnings are :
Local declaration of 'YouTubePlayer' hides instance variable
'SecondViewController' may not respond to '-findButtonInView:'
You should call [YouTubePlayer view] in the findButtonInView-method.
In your SecondViewController.h-file, add the line - (UIButton *)findButtonInView:(UIView *)view;.