UIViewController Edit button status always same - iphone

I added an Edit button to Navigation bar in my app.
I have a UIViewController which implements UITableViewDataSource and UITableViewDelegate.
I added the UITableView using storyboards and made connections to data source and delegate.
I did like this in implementation file of the view controller:
self.navigationItem.rightBarButtonItem = self.editButtonItem;
[self.editButtonItem setAction:#selector(setEditing:animated:)];
and
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
DLog(#"Editing = %d", editing)
NSLog(editing ? #"Yes" : #"No");
[self.recentSearchList setEditing:editing animated:YES];
}
The problem is that whenever I tap on Edit button, the BOOL variable "editing" is always "YES". And for the first it sets the UITableView to editing mode but the Edit button still shows "Edit" label instead of "Done". And the since the parameter is always YES, the table view is never set to normal mode.
I read from an Answer in here: UITableView in edit mode - 'Edit' button doesn't change status
I am assuming the Edit button should change its state itself on tapping. And the parameter in the overridden method should also toggle.
I can write code of my own to set flags in the UIViewController to check the mode and toggle the tableView accordingly but I am assuming that there must be some other way.

From the documentation
Also remove the line [self.editButtonItem setAction:#selector(setEditing:animated:)];
The editButtonItem gets implicitly associated with the method - (void)setEditing:(BOOL)editing animated:(BOOL)animated.
So you just gotta override that method and call super's implementation at the top as the documentation says.

I think if you look more closely you will see that editing is actually a reference to the control itself.
When you call setAction on a UIControl ( your right button in this case ) your target can have one of the following signatures.
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)
You have to handle edit mode within this method separately.

Related

How can you program a UITextField's keyboard to open on viewDidLoad?

I want my UITextField's keyboard to stay open for the entire time I use this view controller. I don't want it to only open when my user touches the text field. In order to do this, I was hoping I would call the textFieldShouldBeginEditing method by doing this:
EDIT: thanks everyone, I just noticed I called my UITextField a UIImage field for some reason in the interface.
The textFieldShouldBeginEditing delegate method is not something that you call from your code. The OS calls the method when that particular event occurs, and you put code in there to run when the event is fired (similar to putting code in viewDidLoad for your view controller).
To show the keyboard whenever the view controller appears, simply call the UITextField's becomeFirstResponder method in the view controller's viewDidAppear method like this:
[self.myTextField becomeFirstResponder];
Don't forget to create an IBOutlet parameter for the UITextField, link it in Interface Builder, and replace self.myTextField above with the outlet that you created.
You should trigger your textview in viewDidAppear method:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.myTextField becomeFirstResponder];
}

How can I bring keyboard to edit a field when a UIViewController is shown?

I want to show the keyboard just after a view controller is being pushed to start editing a specific UITextField.
I believe that I should manually fire the event on the ViewDidAppear.
Which is the proper way of doing such tasks?
To make keyboard appear you need to manually set your text field as a first responder:
[textField becomeFirstResponder];
It can be called either in viewWillAppear: or in viewDidAppear: method - whichever provides best behaviour for you.
for that you need IBOutlet UITextFiled *yourTextField;
- (void)viewDidLoad or viewWillAppear:(BOOL)animated or viewDidAppear:(BOOL)animated
{
[yourTextField becomeFirstResponder];
}

Open UITableView after Click on Button

I have a Tabbar - Application and i want to show data (NSString) from a NSMutableArray in an UITextfield or better in an UITableView after a button-click.
how can i init and open and fill the UITabelView?
regards
That seems like a basic implementation of a UITableView. The Apple UITableView documentation should cover any questions you have on UITableViews. For your button click, tie the IBAction of the button to call [yourTableView reload]. This forces a load of the table view.
Are your tableView and textField in the same tab where your button is?
Say you have stringArray which is holding all the NSString Objects.
then you can set
textField.text = [stringArray objectAtIndex:indexYouWant];
If you want to add the whole array to tableView, you will need to use that stringArray and implement tableView Methods.
But still I am not clear with your question.
This is an overly broad question. Why not look at some example code and try to figure it out? If you get stuck, come back and post a question and some code showing where you are having a problem.
You need to know the Life Cycle of UIViewController and its UIView
Concentrate on the methods that are responsible for the life cycle of the UIViewControllera:
Creature
Init
initWithNibName:
Creating view
(BOOL) isViewLoaded
LoadView
viewDidLoad
(UIView *) initWithFrame: (CGRect) frame
(UIView *) initWithCoder: (NSCoder *) coder
Processing state change view
viewDidLoad
ViewWillAppear: (BOOL) animated
ViewDidAppear: (BOOL) animated
ViewWillDisappear: (BOOL) animated
ViewDidDisappear: (BOOL) animated
ViewDidUnload
Processing memory warning
DidReceiveMemoryWarning
Destruction
ViewDidUnload
was given
Proceeding from this in one of the initialization methods after clicking on the button, you must fill out the table and call after the reloaddata

Managing rows in view based application

I was able to manage rows(add, delete and reorder) of an uitableviewcell in navigation based application, but it is not working in a view based application. The edit button that created in navigation type application is making it possible to edit. Anyone knows what action method is called when the touch up inside of this navigation button occurs? What is happening on this method is that the provisions for adding, deleting and reordering rows coming up on this action, but i've not written any action method of this. SO is there any similar way in a view based application to do these things??
The UIViewController is having its setEditing:animated: method called by that nav bar button. If you're hooking up a regular button, your handler should call that method, and the method should look something like this:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated]; // must be called first according to Apple docs
[table setEditing:editing animated:animated];
}
i.e. your controller will tell the table to also go into editing mode.

How to detect edit mode on iPhone UITableView

For my iPhone app, I have an editable (for delete) table view. I'd like to be able to detect that the user has clicked the "Edit" button. See this image: http://grab.by/It0
From the docs, it looked like if I implemented :
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
then I could detect it (although from the name of the method, I wouldn't think that). This proved not to work.
Any ideas on detecting this? The reason I want to is I want to hook up a "Delete all" button in the upper left hand corner when in delete mode.
thanks
It is probably not working as you expect because willBeginEditingRowAtIndexPath: is called before the editing starts.
If you want to check while in another method you need the editing property:
#property(nonatomic, getter=isEditing) BOOL editing
If you want to do something when the 'Edit' button is pressed you need to implement the setEditing method:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
Which you'll find in UIViewController. (Well, that's the most likely place; there are others.)
Swift
Use below code accordingly:
open var isEditing: Bool // default is NO. setting is not animated.
open func setEditing(_ editing: Bool, animated: Bool)
When subclassing a tableviewcontroller (what most people are going to be doing most of the time since you have to override it's delegate methods just to put data into it...) you can just override the setEditing:animated: method to grab editing state changes.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
NSLog(#"Editing %i", editing);
[super setEditing:editing animated:animated];
}
That passes the state change along to the super class, but lets you jump in the middle and detect the change, or alter it if you wanted...
The setEditing:animated: examples didn't work for me (on iOS 6.1) to detect the state changes that occur when you enter and exit delete confirmation mode. It seems that setEditing:animated: is only called once, when the table view goes into edit mode, but not on state changes of the cells. After some debugger fun, I arrived at a method to detect the cell state change.
My use case is different from yours. I just wanted to hide the label when the delete button is showing so that the other cell content doesn't overlap it when the Delete button slides in. (I'm using UITableViewCellStyleValue2, the one with the blue label on the left and black label on the right.)
(In your UITableViewCell subclass)
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
if (state & UITableViewCellStateShowingDeleteConfirmationMask) {
// showing delete button
[self.textLabel setAlpha:0.0f]; // <-- I just wanted to hide the label
}
}
- (void)didTransitionToState:(UITableViewCellStateMask)state {
if (!(state & UITableViewCellStateShowingDeleteConfirmationMask)) {
// not showing delete button
[self.textLabel setAlpha:1.0f]; // <-- show the label
}
}
Kendall 's answer works. I did it in following way.
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
NSLog(#"Can edit %d", tableView.editing);
if (tableView.editing == 1) {
[self.editButtonItem setTitle:EDIT_BUTTON_TITLE];
}else {
[self.editButtonItem setTitle:DONE_BUTTON_TITLE];
}
return YES;
}
That method tells you when a user is editing a Cell, not put the table into editing mode. There is a method called when editing mode is entered, to ask each cell if it can be edited:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
I don't think overriding setEditing:animated: makes sense, since you would have to subclass UITableView which is extra work and a class you need for no other reason, not to mention it would have to communicate the fact that editing had been turned on back to the controller.
One other option is to simply add the Edit button yourself - it's a built in UIBarButtonSystemItem, you can add it and then have it call your own method in which you do something specific then call setEditing:animated: on the UITableView itself.
The idea behind editing is that when editing is enabled, each cell is told to go to edit mode, and as asked if there are any specific editing controls that should be applied. So in theory there's no need to detect entry into editing mode beyond changing the appearance of cells. What are you trying to do when editing mode is entered?