global variable that returns a true or false - objective-c

can anyone show me how to implement a var that displays a true value on user input and this value can be used in other controllers to add a value to a different controller...
i would like to use this code to signal arrays to add objects. If anyone has a better way to implement this I would appreciate that too.
I have an ibaction that requires a uibutton to be clicked.
When this button is clicked i want a variable to be triggered.
add favorites lets say.
When this variable is triggered i want to go to another controller
and say if variable = true add this object to the array.

If you want the changes in a property to trigger things to happen in other objects I think Key-Value Observation is what you need. Or you couild just call a method on your other controller from the action.

Related

Does setNeedsDisplay:NO have any use at all?

In Cocoa, when we want to redraw a view, we would send the view a setNeedsDisplay: message telling the view to redraw itself with a parameter of YES. I was wondering if there are any circumstances where you would want to send a view setNeedsDisplay:NO, such as multithreading environments, and if sending a view a setNeedsDisplay:YES, then setting it again immediately after with setNeedsDisplay:NO would make the view redraw itself. If there are no reasons to call setNeedsDisplay:NO, then why create such a tedious method, where they could instead implement something like [view redrawView]
setNeedsDisplay:NO may be used in case you want to discard previously called setNeedsDisplay:YES. E.g. sometimes it is easier to mark all subviews as needing display and then run an algorithm to unmark some of them.
As you perhaps know, the display update is automatic (if necessary) at each pass through the normal event loop. You call setNeedsDisplay: in order to force a display update in between if it is necessary.
From the documentation of NSView:
Discussion
Whenever the data or state used for drawing a view object changes, the view should be sent a setNeedsDisplay: message. NSView objects marked as needing display are automatically redisplayed on each pass through the application’s event loop. (View objects that need to redisplay before the event loop comes around can of course immediately be sent the appropriate display... method.)
The boolean parameter to this function simply specifies if the entire bounds of the view in question is affected or not, not if some property "needsDisplay" is set to true or false. Thus, setNeedsDisplay: does indeed work pretty much like a "redrawView", only with the additional parameter.
Edit
The above was inspired from the same documentation:
flag
If YES, marks the receiver’s entire bounds as needing display; if NO, marks it as not needing display.

How to avoid extra calling of -textFieldDidEndEditing in some cases in objective c?

I have a tableview with customcells with textfields in it. I am facing a peculiar problem now:
When I tap on first row textfield, -beginEditing gets called.
Now I change the value and tap on second row textfield. So, the -didEndEditing of first row gets called. In this didEnd, I have some parsing methods which are called in some other class. But they are not executed now. Right after the didEnd, -beginEditing of second row text is called. After that the parsing happens. Till now, it is fine.
When the parsing is finished, objects from parsing is set in other classes,the flow should stop here, but I don't know from where and why, The -didEndEditing for the second row gets called ! Also, though any resignfirstresponder is not written anywhere, the keyboard gets dismissed !
Any clue why is this happening and how to solve it ?
This is the way Apple designed the system - all developers have to deal with it (right or wrong). The key is that you are given the "textField" property so you know WHICH one of the textFields is sending the delegate messages.
The solution is to use one or more mutable dictionaries (or some data structure) to keep state for each individual textField. You can have a primary dictionary that uses the textField object as the key, then for each textField a dictionary that has the current state, and any other info you want to retain about it.
You can probably hack a less elegant but easier to code solution to. In any case, there is overlap on these messages and there is not way to avoid it.
EDIT: use the tag as the key, or create a non-retained NSValue pointer object, but don't use the text field itself.

How to change the size of row in UIPickerView by button click?

I tried to do it in this way: call reloadAllComponents: when button is clicked but it doesnt call rowHeightForComponent:
From the documentation, it looks like reloadAllComponents: just calls the delegate for new data inside the components, but presumably doesn't ask for the size.
Instead, I would try calling rowSizeForComponent: on each component. In the documentation for this method, it says, "Returns: The size of rows in the given component. This is generally the size required to display the largest string or view used as a row in the component. A picker view fetches the value of this property by calling the pickerView:widthForComponent: and pickerView:rowHeightForComponent: delegate methods, and caches it. The default value is (0, 0)."
Then, in your UIPickerViewDelegate, I would implement both pickerView:widthForComponent: and pickerView:rowHeightForComponent:.
You don't need to release pickerView to set new cell height like #auspicious99 proposed.
Instead set the delegate or dataSource each time you trigger reloadAllComponents like this:
pickerView.delegate = self; //or pickerView.dataSource = self;
[pickerView reloadAllComponents];
It's really strange why reloadAllComponents methods doesn't trigger rowHeightForComponent method, but this looks like to be a valid workaround using above trick.
Hope it helps.
You might need to release the UIpickerview and create another one with the appropriate return value in pickerView:rowHeightForComponent:
This seems like a pain, but I can't think of another solution offhand.
Unfortunately, it seems that calling rowSizeForComponent won't help, because, as the docs say, "A picker view fetches the value of this property by calling the pickerView:widthForComponent: and pickerView:rowHeightForComponent: delegate methods, and caches it." So, calling rowSizeForComponent only provides the cached values, rather than calling pickerView:rowHeightForComponent: to pick up the new value.

Cocoa: getting a Table View cell to send action messages

I'm really having trouble getting a Cocoa Table View cell to send action messages.
At the most basic level, in IB there is an action assigned for the NSTextViewCell object, and after editing and pressing Return nothing happens.
So I have an IBOutlet hooked up to the NSTextViewCell, and have been experimenting with NSActionCell messages to it. But the Table View seems to pretty much just ignore them.
I've also tried subclassing NSTextViewCell, but the methods I'm seeing all look like they want to pass values to the object from somewhere, not return a value from inside the object to configure its behavior.
I'm pretty new to programming and Cocoa -- can someone explain each thing that needs to be overridden and how and where to do it?
AFAIK, the cells in an NSTableView won't send action messages out to your application, they're sent to the NSTableView so it can update its data. NSTableView itself tries to be pretty clever and update your data directly, rather than just telling you something changed, so depending on what you're trying to do and what the data source for the table is, you have a few options.
If you're using an NSTableViewDataSource object to populate the table, it's simple; just implement tableView:setObjectValue:forTableColumn:row: and the NSTableView will call that every time something is edited.
If you're using Cocoa data binding (for example, using an NSArrayController to bind an array of objects to the table,) then as long as everything is wired up correctly, the data should just automagically get updated in the source objects when the table is edited. If you need to take special action, then you can do whatever you need to in the property setter of your data class.
I haven't tried it yet, but could work...
NSCell *cellYouWant = [tableView preparedCellAtColumn:tableView.clickedColumn row:tableView.clickedRow];

How do I populate an NSPopupButton with an array of strings?

Simple question, but I am struggling a little to understand how Interface Builder interacts with the rest of the program developed in X-Code.
My UI has an NSPopupButton that I would like to fill with an array of strings that I provide. I have a controller class that defines the method to execute when the button is clicked. Is that where I would populate the NSPopupButton? It seems like I would need to initialize is somewhere, but I am confused about exactly where to do it. I know I need to use addItemsWithTitles, but where do I call that?
Assuming the list isn't changing over time, awakeFromNib (in the controller) would be a good place to populate the menu. If it's dynamic, then you'd want to populate it when the source of those strings changes.