Changing Label from different views in Objective C / Xcode - objective-c

I got an app with one main view and 5 other table views. Selecting row in each table changes some Labels in main view. Is it making main view as delegate of 5 table views right way to choose or there are other more elegant ways? Please, I need just ideas, no code lines. Thanks

it will be better if 5 tables will be present in other class. This will separate your code and code complexity will be less.
For labels, just declare one string in app delegate as
//in appdelegate.h
#property (nonatomic, strong) NSString *lblName;
//in appdelegate.m
#synthesize lblName;
Then,
In the class where tables are you can acess the label as
AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
then label text will be set as, app.lblName=#"Something";
You can access lblName in any class. Just you have to create share instance of app delegate using which you have to access lblName and other strings which you want to access.
Finaly when you come on the main view you can set the label text as
Your_label.text=app.lblName;

Related

What is Controlled in a TableView?

I am experimenting creating views that contain View based TableViews, ie: NSOutlineView and would like to better understand the correct connections that are made within IB.
At the moment, I have created a MainWindow that has the AppDelegate assigned to it.
On top of this, I have introduced a NSOutlineView which ultimately have an array as its data source.
I would like to have a separate Controller to look after this and its sub-views.
The NSOutlineView is made up of:
NSTableColumn
NSTableViewCell
NSTextField
With my NSTableCellView I have added two NSTextFields, the top one will be used as a Title and the second is a mini and will be used for displaying descriptions.
The issue I am facing is what controls need to have an IBOutlet connected to it.
My logic is; the only controls that will exhibit a change are the NSTableView as there needs to be a separate TableView within the TableColumn which will be a result of the item.count in the array.
Secondly, I am assuming that an IBOutlet will need to point to each of the TextField’s in the TableViewCell which will allow them to display the correct information for each item.count in the array.
So, all I need to do is design a Controller that has three IBOutlets.
Have I got my basics right….. or am I way off track?
I don't get everything you are asking for but first of all you don't set outlet to your NSTableView you need to use a NSArrayController, KVC and bindings.
1) in your AppDelegate.h create an IBOutlet to your NSArrayController:
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
IBOutlet NSArrayController *yourArrayController;
}
2) in AppDelegate.m put your input or whatever in the NSArrayController:
yourItem *newItem = [[yourItem alloc] init];
[newItem setValue:[_inputVar stringValue] forKey:#"name"]; //use Key Value Coding
...
[yourArrayController addObject:newItem];
3) now go to your .xib and draw a NSArrayController object (green bubble with 3 blue cubes) in the dock (the area left to the .xib).
4) in your tableview click three times at each row and open the Bindings inspector
5) go to "Value" and activate "Bind to", select yourArrayController
6) set Controller Key to "arrangedObjects"
7) write the appropriate key in Model Key Path
Do that for each tableview column and you should be fine basically. No IBOutlet from the controller to you NSTableView!
The NSArrayController is taking care about user input back an forth to and from your tableview which is pretty convenient. I know not to answer all your questions but it might help you to get started. Good luck!

Is it a good practice to subclass a UIViewController's subclass?

Say you have a 2 subclass of tableView controller.
They both have the same header and footer view on top of the bottom of the header. They both implement pull to refresh.
They both have some common features.
The only different is one is for displaying the whole businesses, the other is for displaying only businesses you bookmark.
It looks like they both need to have the same parent class and the different is resolved on the child class. The differences are minor anyway.
I suppose the parent has it's own XIB, the children has it's own XIB.
Hmm... How would that work out? With the exception of container UIViewController, each controller should view a fullview of content. So which view should we display? The child or the superClass? Should child view add it's superclass subview?
Anyone have ever tried that?
Any code sample on the web that use this approach?
It sounds like, based on the business logic you explain, that everything is in common, except the list of data you're presenting. You could expose a property on your UITableViewController subclass to set the business objects that your tableview presents:
#interface JTBusinessesTableViewController : UITableViewController
#property (nonatomic, strong) NSArray *businesses;
#end
The code that instantiates this class would set the business objects:
JTBusinessesTableViewController *businessListings; //Instantiate from XIB or Storyboard
businessListings.businesses = [self bookmarkedBusinesses];
[self.navigationController pushViewController:businessListings animated:YES]
The code for displaying all businesses isn't going to be much different:
JTBusinessesTableViewController *businessListings; //Instantiate from XIB or Storyboard
businessListings.businesses = [self allBusinesses]; // Here we assign all of them
[self.navigationController pushViewController:businessListings animated:YES]
You're just selectively giving this view controller, the business objects to display.

Is it possible for Multiple UIViewControllers to Share the same code?

I'm making a app with mutiple view controllers, one moves on to the next and once the user moves on to the next view there is no need to go back.
I have created the .m .h files for the view controllers that I need to play sounds on.
What I would like help with is disabling copy/paste/define in UITextview which I know how to do (and works fine) using
#interface MyUITextView : UITextView {
}
#end
#implementation MyUITextView
- (BOOL)canBecomeFirstResponder
{
return NO;
}
Is there a way of using this code once with out creating .m .h files for each and every uiveiwcontroller?
Or will Xcode allow me to assign the same name to more than one uiveiwcontroller and use the same piece of code?
Is it possible to write it into the AppDelegate for every UITextview on different controllers?
Thanks for any help in advance :)
You don't have to duplicate any code. If you're making your UI using xib files or a storyboard, just set the class name of any text views that you want to have behave this way to MyUITextView in Xcode's Identity inspector.

Core Data document based multiple nib based on selection

I'm building a Core Data document based application. In the app there's a main view (NSTableView) and an inspector view. Both views are separate nib files. The content of the inspector view should depend on the selected row in the main view. I have three controller files:
DocumentWindowController (master controller)
ItemsViewController (main view)
SchedulesViewController (part of the inspector)
Every controller owns a nib file. How is it possible to change the content of the inspector when the user selects another row in the main view's table?
I tried to make it work as follows:
Every controller has it's own NSArrayController, which is set up in the DocumentWindowController. The DocumentWindowController gets its managedObjectContext from [[self document] managedObjectContext]
When a user clicks a row in the ItemsViewController's tableView, an NSNotification is posted, with [NSTableView selectedRow] as a parameter. Then a fetch request is being made.
My intuition says I have to do this with Cocoa bindings.
I searched on Stackoverflow and google but I just can't make it work. If everything is in one nib file is very easy to accomplish. What is the best/most used way to achieve this?
Thanks in advance!
You could follow the same paradigm as the table view and create a data source for your inspector view.
#protocol InspectorDataSource <NSObject>
- (void) inspectorView:(InspectorView*)inspectorView managedObjectSelected:(NSManagedObject*)managedObject;
#end
Your inspector view controller would then have the data source as a property.
#interface InspectorViewController : UIViewController{
#public
id<InspectorDataSource> dataSource;
}
#property (nonatomic, assign) id<InspectorDataSource> dataSource;

Accessing Views created in Interface Builder

I've created a ChildViewController class, and then a nib that uses that class.
Then I created a BaseView, that includes some buttons, and some text that I'll be changing programmatically.
Then I created two more views (Boy and Girl), that I want to be able to lay behind the baseview so that the background color is different along with some graphics in an ImageView. I've named the views that I created in IB 'Boy' and 'Girl'...
But when I go back to my code where I'm calling ChildViewController, I'm not sure how to access the views I created so I can call insertSubView. Do I need to instantiate them in code? (in ViewDidLoad perhaps?) Does the nib create the instances when it loads?
I'm confused about how to handle multiple views for a single ViewController
edit =================
#Pablo Santa Cruz
Your answer assumes that i have two nibs and two view controllers (one for each view). I want to know if I can use one nib and one controller, and load in UIViews. It seems silly to create another nib and controller, when all want to do is change the background color and some graphics. Can't I programatically load in UIViews into a UIViewController?
Add IBOutlets in your App Controller class in Xcode then link them in IB (ctrl-click or right-click) from the connections tab in the Inspector to the object.
Then you will be able to send method calls to the objects.
The code in Xcode should look like this:
#interface AppController : NSObject
{
IBOutlet Girl girlIvarName1;
IBOutlet Boy boyIvarName2;
}
#end
You can access a UIView programatically by assigning a value to its tag property, which can be set in IB on the first tab of the inspector (Command 1)
The tag value defaults to zero, so if you want to access it specifically, make it non zero and unique. e.g. 100, which I will use in the example code below
Once the tag is set you can access the view using the following code in your UIViewController that was initWithNibName for the NIB containing the tagged view
UIView *aView = [self.view viewWithTag:100];
You can get instances for your IBuilder views with this piece of code:
boyViewController = [[BoyViewController alloc] initWithNibName:#"BoyViewController" bundle:nil];
girlViewController = [[GirlViewController alloc] initWithNibName:#"GirlViewController" bundle:nil];
Assuming your NIB file names are BoyViewController and GirlViewController. With those instances, you can do whatever you need to. I.E., adding them to a parent view (with addSubView message on the parent).