Change int value from another ViewController - xcode

I have an int value in my file1ViewController.m and i want to change this value form my file2ViewController.m. I tried to import the file, NSUserDefaults but i wasn't able to let it works.
Anyone can help me with the code? What is the simplest method to do this simple thing?
Thanks everyone :)

The easiest method is to pass a reference to a file1ViewController object to a file2ViewController when it is pushed or presented. The exact way of doing that depends on the relationship between those objects and how they are created and given control of the display.
Knowing the difference between classes, objects, and code files is a big step in the right direction.

Related

How to Fix Error: Incompatible Pointer Types Assigning to

Let me preface this by saying I'm new so I might not be the best at asking questions but here it goes. I am getting this error when I hit build in XCode with this code.
-(IBAction)unwindToRecipeDetails:(UIStoryboardSegue*)segue
{
ChangeRecipeDetailViewController *source = [segue sourceViewController];
recipeLabel = source.textView; // error: Incompatible Pointer Types Assigning to UILabel from UITextField
}
Here's what I'm trying to do. I based the code off of this snippet which works...
-(IBAction)unwindToRecipeBook:(UIStoryboardSegue *)segue;
{
AddRecipeViewController *source = [segue sourceViewController];
RecipeItem *item = source.recipeItem;
}
This is supposed to make AddRecipeViewController the data source for the initial RecipeBook page when the user navigates backward. I'm trying to implement the same thing for when a user navigates backward from the ChangeRecipe page to the RecipeDetails page.
Apparently I mixed two different types of code in the error, UILabel and UITextField. I get that they are incompatible but what could I change either one to in order to make it work. It is a little different than what I am basing my code off of in that I don't just want to transfer the same text, I want to change its format as well.
Again, I'm new at this so if anyone needs more information I'll gladly post it. Thanks.
Update 1: Palpatim: Thank you. Your solution worked. So to clarify, in order to transfer text I have to put .text after the initial source. This combines two unlike objects and transfers only text to text. Is this correct? I'm new at coding and am just trying to learn.
Philip Mills: I meant I wanted to change the format of the text when I transfer it from something that is editable like a textview to something static like a label.
Thanks for the help.

Is there a QWidget which visualizes something like this "first |1|2|3...30|31|33| last"

I suppose everyone has used these, but I don't know how they are called, so I couldn't really search for them. Their behaviour is as follows: one can click on "first" or "last" or anything in between to navigate to a certain page.
Does anyone know if there exists some kind of QWidget which does that? Maybe similar to a combo-box where one can select something and a signal with the selected index is emitted?
Any hints would be much appreciated.
Thank you for your time david
The short answer is no, QT does not provide a widget with the capabilities you are looking for. It should be relatively simple to recreate using QButtonGroup and a styled set ofQPushButtons or re-implemented QABstractPushButtons.

Simplest way to identify an Interface Builder element in code

I am building a UI in Interface Builder and am looking for the simplest (least code) way to identify an element from code.
I'd like to avoid using outlets because frankly I detest visual programming and don't want to pollute my class space with countless outlet properties. Is there some unique string identifier I can assign to static elements that I can either reference directly or easily look up from code?
Ideally I just want to look up an object by its id like I can do in JavaScript:
document.getElementById('myIdentifier');
I agree with rightfold that outlets are the best solution, but there is an answer that addresses your question directly: you can use the (integer) tag property of UIView (setting it either in IB or in code), and then you can fetch the view with the method UIView -viewWithTag:.
Successive calls to -viewWithTag: will iterate through the subviews that have the given tag. Because it's an integer, you'll probably want to use named constants for tag references in code, but unfortunately there's no way (that I am aware of) to use constants in that manner in IB.
The default value for the tag property is 0, so avoid using that as a semantically meaningful tag.

BoxLayout in Cocoa

Originally, I wanted to ask how to create user interfaces with cocoa programmatically, i.e. without using interface builder. But it seems that someone else has already asked this question and the answers didn't help me.
So I'll ask a different question, that I hope will indirectly help me answer the previous question. Here it is:
(QUESTION_START)
How do I create an Objective C class that is equivalent in functionality with the BoxLayout class in Java? (Just click the link, the image on that page says everything you need to know about BoxLayout.)
(QUESTION_END)
Any help in the right direction will be appreciated!
There are a few sub tasks that are connected with the question, e.g.
"How do I ask a user interface element (e.g. a button) how large it wants to be" (before it has been drawn to the screen). To draw it on the screen you have to already know its size, don't you? Obviously, the interface builder application has figured out a way to do this.
I know that many Cocoa developers think it's a stupid idea to even try what I want to do. Let me tell you: I know the reasoning behind that opinion. Right now, laying out controls without interface builder sucks, because there is nothing that comes even close to a layout manager in cocoa. But if you think my question is stupid, please DONT answer. The whole internet is full of explanations why you would never want to create UIs with code in cocoa.
Thanks!
Answering your first question is kind of difficult and fairly involved, so I'm going to dive into your subquestion first:
How do I ask a user interface element (e.g. a button) how large it wants to be?
When you create a UI element, you tell it how big it should be (as well as where it should be) via its initWithFrame: constructor; or you can set its frame later via its setFrame: method. It will then draw itself into that space. You can get an element's frame via its frame method.
With that in mind, a BoxLayout class would, hypothetically, be a controller of some sort in which you could add UI elements, and then the BoxLayout controller would arrange them in a grid (or whatever) on an NSView of some sort.
I know you weren't looking for answers that questions motives, but given the complexity of a BoxLayout class vs. laying out the interface in IB, it seems relevant to ask why you want to do this.

Custom editor in QAbstractTableModel

Does anyone have an example of using a QWidget as an editor in a QAbstractTableModel?
I have a column which when edited should create a QCombobox with the list of choices.
The docs seem to suggest I need to write a QAbstractItemDelegate and a custom paint function but that seems overkill to simply pop-up a standard QCombobox in Qt::EditRole.
Note - the combo box contents are the same for every row and it only needs to be shown when somebody clicks in the cell.
I know this should be simple but I can't get it to work. It's easy for a QTableWidget based table - but I need it for a very large data table.
The docs seem to suggest I need to write a QAbstractItemDelegate and a custom paint function but that seems overkill to simply pop-up a standard QCombobox in Qt::EditRole.
You don't need to go that far. One way is to subclass QStyledItemDelegate and then override createEditor() so that it returns your prepopulated combo box. Its setEditorData and setModelData functions will probably already suffice if you`re using basic Qt value types.
If you need something more generic that works across many different models, you can create a QItemEditorFactory that associates your editor with the correct type. This also works well with custom types.
When indicated by your view's EditTrigger, your view will get the delegate specific to the cell on which the edit is being invoked and call delegate->createEditor(...) which can then size the combo box according to the options parameter as well as set the current entry to the value specified by the model, although most of this should be handled by the QStyledItemDelegate. Thus, you won't have to worry about the Qt::EditRole directly as the view will handle that.
Did you try and have a look at the following example from Qt :
Spin Box Delegate Example
Maybe it will give you a much clearer view on the subject !
Hope it helps a bit !

Resources