UIPageControl value changed not firing - objective-c

I have an issue with the UIPageControl. I simplified the issue for clarity below:
I put a button and a UIPageControl in my app with Interface builder and put the following in the code:
- (IBAction)tappedButton:(id)sender{
self.pageControl.currentPage = 3;
NSLog(#"Tapped Button");
}
- (IBAction)changePage:(id)sender{
NSLog(#"PAGE Changed!!!!");
}
I attached the Value Changed action to the pageController via Interface Builder.
When I tap on the button, I see the output "Tapped Button" and the third dot is highlighted... but the changePage method is never called.
Any ideas?

The changePage: method will only be called when the users request a change of page using the UIPageControl. It won't be called when the user taps the button. If you want to call the changePage: method when the button is pressed called it explicitly:
[self changePage: nil];

UIPageControl doesn't actually change pages for you. You have to write that code, inresponse to the valueChanged event. So, you want to connect that event of your UIPageControl object to your output (tappedButton: I guess), and then call your changePage: method to actually change the UI. Does that help?
There are some example projects referenced from the Apple docs that use UIScrollView and UIPageControl together. Check 'em out.

Related

How to bind click action of NSButton in view based NSTableView

I have an NSTableView that is set to be 'view based', and within each NSTableCellView there is an NSButton and an NSTextField.
The text field is being populated correctly from an array controller. The buttons are appearing correctly but I'm having trouble working out how to hook up the click action.
I thought this would be possible by control-dragging from the NSButton in IB to a simple method like this one in my controller (in this case an NSDocument subclass):
- (IBAction)testAction:(NSButton *)sender {
NSLog(#"Test action");
}
It connects fine but never gets fired. Any ideas why this is or how to fix it?
I don't understand why this works, but I had the same problem and was able to get it working by assigning the table delegate and datasource to the file owner within IB, which is also the class of my click handlers. Only then did it seem to actually bind the click handlers for the buttons in my cell view. Previously I was setting the delegate and datasource in code after the view was loaded.
You have to subclass NSTableCellView class. put your onClick Action method in subclass files.
Let me know if i am not clear..

UIButton Not Loading Properly For MKAnnotationView

I am creating an MKAnnotationView with a detail disclosure button.
In mapView: viewForAnnotation: I just create an placeholder button.
// the right accessory view needs to be a disclosure button ready to bring up the photo
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
In mapView: didSelectAnnotationView: I actually create a button to be used (with the relevant tag)
// create a button for the callout
UIButton *disclosure = [self.delegate mapController:self buttonForAnnotation:aView.annotation];
NSLog(#"DisclosureButton: %#", disclosure);
// set the button's target for when it is tapped upon
[disclosure addTarget:self.delegate action:#selector(presentAnnotationPhoto:) forControlEvents:UIControlEventTouchUpInside];
// make the button the right callout accessory view
aView.rightCalloutAccessoryView = disclosure;
In the log, the button appears to be fully instantiated as well as set with the correct tag.
This is the button creator:
/**
* returns an button for a specific annotation
*
* #param sender the map controller which is sending this method to us (its' delegate)
* #param annotation the annotation we need to create a button for
*/
- (UIButton *)mapController:(MapController *) sender
buttonForAnnotation:(id <MKAnnotation>) annotation
{
// get the annotation as a flickr photo annotation
FlickrPhotoAnnotation *fpa = (FlickrPhotoAnnotation *)annotation;
// create a disclosure button used for showing photo in callout
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// associate the correct photo with the button
disclosureButton.tag = [self.photoList indexOfObject:fpa.photo];
return disclosureButton;
}
The problem comes when I select the annotation. For a few seconds when the annotation is selected and the detail disclosure button is tapped, nothing happens. However, after tapping away and back onto the annotation a few times and testing the button, it eventually works as expected.
What is going on with the strange delay? Sometimes when the button is going to work, it just appears as if the alpha is set to 0.0 until you tap on it and it appears.
Seriously one of the more odd problems I've encountered.
Before the didSelectAnnotationView delegate method is called, the map view has already prepared the callout view based on the annotation view's properties (before your changes).
So the callout you see the on the first tap is without the changes the app makes in didSelectAnnotationView. On the following taps, the callout could be based on the values set from the previous tap (this actually depends on how annotation view re-use is handled in viewForAnnotation).
It looks like the only things the code is doing in didSelectAnnotationView and buttonForAnnotation is setting the button action and tag.
I assume you're using the "tag" approach because the presentAnnotationPhoto: method needs to reference the selected annotation's properties.
You don't need to use a tag to get the selected annotation in your action method. Instead, there are a couple of better options:
Your custom action method can get the selected annotation from the map view's selectedAnnotations property. See this question for an example of how to do this.
Use the map view's own delegate method calloutAccessoryControlTapped instead of a custom action method. The delegate method passes a reference to the annotation view which contains a property pointing to its annotation (ie. view.annotation) so there's no guessing, searching, or question as to what annotation was selected. I recommend this option.
In the first option, do the addTarget in viewForAnnotation and don't bother setting the tag. You also don't need the buttonForAnnotation method. Then in the button action method, get the selected annotation from mapView.selectedAnnotations.
Currently, your action method is on self.delegate so you might have some trouble accessing the map view from that other controller. What you can do is create a local button action method in the map controller which gets the selected annotation and then calls the presentAnnotationPhoto: action method on self.delegate (except now that method can be written to accept an annotation parameter instead of being a button tap handler).
The second option is similar except you don't need to do any addTarget and in the calloutAccessoryControlTapped method, call presentAnnotationPhoto: on self.delegate.
For both options, I suggest modifying the presentAnnotationPhoto: method to accept the annotation object itself (FlickrPhotoAnnotation *) instead of the current UIButton * and in the map controller, do an addTarget on a method local to the map controller (or use calloutAccessoryControlTapped) and from that method, manually call presentAnnotationPhoto: and pass it the annotation.

I cannot hide a textfield and label in my iOS app

here is the low down:
-(IBAction)button1click:(id)sender;
{
label1.hidden=YES;
textfield1.hidden=YES;
label2.hidden=NO;
textfield2.hidden=NO;
-(IBAction)button2click:(id)sender;
{
label1.hidden=NO;
textfield1.hidden=NO;
label2.hidden=YES;
textfield2.hidden=YES;
the is issue is that when i first open my screen all 4 labels are visible. By default button1 radio is checked but label2 and textfield 2 are visible when they shouldnt be. if i press button1 even though it is already selected the items with hide and then all is good. My issue is having them hidden when the screen first opens up.
Thanx all for you help
You can, in your viewDidLoad method:
-(void) viewDidLoad
{
[super viewDidLoad];
[self button1click:nil]; //nil or the instance of button1 if you need it
}
In this way, you will execute the same code when you press button1 without duplicate your code.
You can take one of two approaches to hide the label.
a) in Interface builder you can click the check box for hidden in the attributes inspector. If you do that the default behavior will always be hidden when the app launches then you can make it visible in code like your example shows
b) add your existing code to hide the label to your view controllers - (void)viewDidLoad method.
both methods work equally well.
When you create that objects you can set foo.isHidden = YES

Handling 'Done' button on the keyboard outside of textViewDidEndEditing

When "Done" is pressed I know textViewDidEndEditing: is called. It is also sometimes called by other actions.
I want a method that is only called when "Done" is pressed -- exclusively. I've been reading around for snippets of code, but don't see anybody doing this. I am not using any XIB for my view, by the way.
Is this possible?
An instance of UITextField will send a textFieldShouldReturn: message to its delegate whenever the Return key is pressed. If you want your controller to receive this message, have it send a setDelegate: message to the text field, passing self as the argument (or connect the text field's delegate outlet to the controller in Interface Builder) and implement the following method in your controller class:
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
See the documentation for the UITextFieldDelegate protocol for further information.
Firstly, the textFieldDidEndEditing is called not when the user taps the DONE button. It is called as soon as you touch anything after editing.
For having an exclusive button 'Done' you can create a Done key as a UIBarButtonItem, and write an IBAction like
- (IBAction) doneClicked:(id)sender
The following line assigns the Done key as the Return Key.
textField.returnKeyType = UIReturnKeyDone;
You can check if the Done key was pressed in the TextFieldDidEndEditing...You may also use another button, and return it instead.

Why is it that my UITableViewController setEditing:animated: method is not called?

I must be doing stupid, but I can't see what: my UITableViewController subclass is never called when the edit button of my navigation is pressed.
What could be causing that?
My view hierarchy is loaded from a Nib file and put inside a popover. The [+] button is connected to the insertNewObject action of my UITableViewController subclass. It works fine.
The [Edit] button however has no action to connect to. The doc says it will automatically call the setEditing:animated: method of the view controller, which I override.
The nib file is set up pretty much as usual AFAICT. And in fact, I'm not sure what additional detail I can give that would suggest my mistake.
What is the control flow from the click on the [Edit] button to the call of the setEditing:animated method?
I feel like we must be missing the same thing.
Whatever the case, I made it work by doing the following.
IBOutlet UIBarButtonItem *editButton;
-(IBAction)editButtonPressed:(id)sender {
[self setEditing:YES animated:YES];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
if(self.tableView.isEditing)
{
self.editButton.style = UIBarButtonItemStylePlain;
self.editButton.title = #"Edit";
}
else
{
//self.editButton.style = UIBarButtonSystemItemDone;
self.editButton.style = UIBarButtonSystemItemEdit;
self.editButton.title = #"Done";
}
// Toggle table view state
[super setEditing:!self.tableView.isEditing animated:animate];
}
I hooked the editButton up to the button I added to the nav bar and it's action to the editButtonPressed IBAction. After doing that my setEditing: is called (obviously) and the super call toggles the table view's editing state.
I'd like to use the system defined button styles, but the appropriate one is commented out because while it did change style I couldn't figure out how to change the text from "Edit" to "Done" so I had to do it all manually (that only worked if I left the button as Custom and set the style generically). This has the downside of not being localized (for free), etc.