I am not sure but you may want to check one thing.
button.action = @selector(myaction:);
Are you missing the colon at the end of myaction or it's just a typo? I think whenever you specify a function which will be called, you should have at least one argument that is sender of type id in the signature of the event handling method. Target-action pattern to be precise.
I am not sure but you may want to check one thing.
button.action = @selector(myaction:);
Are you missing the colon at the end of myaction or it's just a typo? I think whenever you specify a function which will be called, you should have at least one argument that is sender of type id in the signature of the event handling method. Target-action pattern to be precise.
Hope this helps..
I don't have the colon but it is not necessary. Using button image or title instead of customView works fine.
Hey Anybody solve that problem?
I am struggling with that for last 3 hours...
Ok I gave up with adding the actions to the UIBarButtonItems in that way. Instead of that I am adding buttons (UIButton) than calling [buttonMenu addTarget:self action:@selector(navigateTo forControlEvents:UIControlEventTouchUpInside];
and finally adding it to the toolbar and everything works great.
Except:
I was trying to automate the process. I have four buttons that have the same size and they will have the same selector. So I have created two arrays, first storing the buttons and the second one storing the images. Than I wanted to set up all buttons inside a for loop:
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemF lexibleSpace target:nil action:nil];
//Adding the items to the array:
It works when I don't use for loop and write everything step by step like:
UIButton * buttonNext = [UIButton buttonWithType:UIButtonTypeCustom];
buttonNext.bounds = CGRectMake(0, 0, 65.0, 30.0);
[buttonNext setImage:next.image forState:UIControlStateNormal];
[buttonNext addTarget:self action:@selector(navigateTo forControlEvents:UIControlEventTouchUpInside];
Ok I gave up with adding the actions to the UIBarButtonItems in that way. Instead of that I am adding buttons (UIButton) than calling [buttonMenu addTarget:self action:@selector(navigateTo forControlEvents:UIControlEventTouchUpInside];
and finally adding it to the toolbar and everything works great.
Except:
I was trying to automate the process. I have four buttons that have the same size and they will have the same selector. So I have created two arrays, first storing the buttons and the second one storing the images. Than I wanted to set up all buttons inside a for loop:
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemF lexibleSpace target:nil action:nil];
//Adding the items to the array:
It works when I don't use for loop and write everything step by step like:
UIButton * buttonNext = [UIButton buttonWithType:UIButtonTypeCustom];
buttonNext.bounds = CGRectMake(0, 0, 65.0, 30.0);
[buttonNext setImage:next.image forState:UIControlStateNormal];
[buttonNext addTarget:self action:@selector(navigateTo forControlEvents:UIControlEventTouchUpInside];
Regards,
Janek
I struggled with this problem too once, and the solution I found was to make a UISegmentedControl with the custom image you want, and then setting the properties of the segmented control and creating a UIBarButtonItem with the segmented control as the custom view. The problem is still that it doesn't look like UIBarButtonItem with a string title, the background color is not the same, but it's better than using UIButton I think. Anyway, here is the code:
Excelent one small fix if you want to see only the image and no button backgr.
Quote:
Originally Posted by fiftysixty
I struggled with this problem too once, and the solution I found was to make a UISegmentedControl with the custom image you want, and then setting the properties of the segmented control and creating a UIBarButtonItem with the segmented control as the custom view. The problem is still that it doesn't look like UIBarButtonItem with a string title, the background color is not the same, but it's better than using UIButton I think. Anyway, here is the code:
Guys,
No need for work-arounds here, the answer is right in the property name, "customview".
Just do the following:
1. Create a UIView with the required frame parameters, i.e 40x40
2. Create a UIImageView initialized with the desired UIImage
3. Create a blank UIButton, but set its frame to that of the UIView. set the action and target on the button - the action should be the desired function call.
4. Add The imageview and UIButton to the UIView
5. Set the UIView to the customview property of the nav button, and you should be all set.
Guys,
No need for work-arounds here, the answer is right in the property name, "customview".
Just do the following:
1. Create a UIView with the required frame parameters, i.e 40x40
2. Create a UIImageView initialized with the desired UIImage
3. Create a blank UIButton, but set its frame to that of the UIView. set the action and target on the button - the action should be the desired function call.
4. Add The imageview and UIButton to the UIView
5. Set the UIView to the customview property of the nav button, and you should be all set.
can you show me the code, cause i try your advice, but the nav button is not show anymore...
Guys,
No need for work-arounds here, the answer is right in the property name, "customview".
Just do the following:
1. Create a UIView with the required frame parameters, i.e 40x40
2. Create a UIImageView initialized with the desired UIImage
3. Create a blank UIButton, but set its frame to that of the UIView. set the action and target on the button - the action should be the desired function call.
4. Add The imageview and UIButton to the UIView
5. Set the UIView to the customview property of the nav button, and you should be all set.
Even simpler: UIButton is a subclass of UIView. Create your button, add your action and target, and use that button as the custom view.
You can also do this in Interface Builder -- drag a button onto the main object window (not a subview to any of your views), drag the Touch UpInside event to the appropriate selector in your File's Owner, and then in viewDidLoad:
slight side issue, but this is for an ipad right? im pretty sure the docs say that there is a max limit for the toolbar on both iOS devices and, i could be wrong, but i believe that 5 is to many for the iPhone, or is it just on the max?
Even simpler: UIButton is a subclass of UIView. Create your button, add your action and target, and use that button as the custom view.
You can also do this in Interface Builder -- drag a button onto the main object window (not a subview to any of your views), drag the Touch UpInside event to the appropriate selector in your File's Owner, and then in viewDidLoad:
Thanks so much. I've been doing this for a while now and this is the first time I've come across the method to use IB to create an object, but not put it in any views. This is *way* easier than doing things programmatically!
I've used IB a lot, but always for things in my views. Other things like UIActivityIndicators I've created programtically, which is a PITA. This method is a snap!