Load Different View Controllers in the Master-Detail App template - xcode

I am developing a master detail application using Xcode 4.3.3, now in the master view controller i have managed to load an additional view controller called "Fav Real"When this view controller loads, it will contain different items taken from an sqlite database into a tableViewController. My problem is here, i need when i click on any loaded row, to load a different viewController into the detailViewController, to load another view controller instead of the first one. I am actually lost, i tried to do the following schema but i have no idea how to do my goal...Any help will be highly appreciated.
As it shows, the items will load correctly in the FavReal tableView , but my goal is when i click at any row, the "Details" showing in the right will load instead of the DetailsViewController.I tried this code in the FavReal.m but i always get : " Application tried to push a nil view controller on target .
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Details * vc4 = [self.storyboard instantiateViewControllerWithIdentifier:#"Details"];
[self.navigationController pushViewController:vc4 animated:YES];
}
Thank you for your time, if any further details are needed please tell me

In case anyone was having the same problem as i was, the only solution i found is not to load a different view controller and replace the original detailViewController, but the actual solution was to create a new storyboard and make it appear when the Favorites button is clicked, this way you'll have a nice stylish effect and keep your data presentation accurate. To do so use the following lines of code :
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
UIViewController* initialHelpView = [storyboard instantiateInitialViewController];
initialHelpView.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:initialHelpView animated:YES];
You have the freedom to change the modal presentation as u prefer.Best of luck

Related

Change UILabel from appDelegate

I want to do some stuff from the appDelegate in Xcode. One of these things are to change a UILabel.
ViewController *viewController = [[UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:#"id"];
viewController.label.text = #"heeej";
I was told this would do the trick. Doesn't work. Label doesn't change. Does anyone know what the problem is?
There are several problems:
Don't do anything in the AppDelegate except for loading the window an the initial view controllers (if needed).
You are instantiating a new view controller in the first line. I assume you already have a view controller and you want to change the label in that view controller. Than you need a reference to that view controller and use this reference to send messages to it.
The label should not be a property of the view controller. You should try to follow the design pattern Model-View-Controller or the pattern Model-View-ViewModel. Ask you preferred search engine what those are if you don't know.
id as an identifier for anything in Objective-C is a bad idea because this is used as an object type.
Edit: You don't need a reference to change a property in the view controller. Use notifications to update the label. The system sends a notification with the name UIApplicationWillEnterForgroundNotification' (see [here][1]). Add the view controller as an observer to the[NSNotificationCenter defaultCenter]` for this name and react on the notification. Read the Apple documentation if you don't know what I am talking about.

Referencing an NSViewController's view from within an NSWindowController's nib in Interface Builder

If you're using NSViewController's, each with their own XIB files, is there a way to refer to those views while inside of an NSWindowController's XIB file? I want to be able to drop a view in my NSWindowController and have Interface Builder understand that that view is actually the view it can get from the embedded NSViewController.
MyWindowController.xib
[Objects]
-- MyViewController (Loaded from MyViewController.xib)
-- OtherViewController (Loaded from OtherViewController.xib)
[Window]
-- View
---- My View
---- Other View
I want Interface Builder to know that My View is really the view from MyViewController. At the moment, I'm having to manually add those views to the window's content view in something like windowDidLoad.
- (void)windowDidLoad {
... snip ...
[self.window.contentView addSubview:self.myViewController.view];
[self.window.contentView addSubview:self.otherViewController.view];
... Configure constraints ...
}
This is a simplified example, but in the larger case, there might be numerous view controllers in play, each with its own view. I'd prefer to setup their initial constraints and layout in Interface Builder but I'm not sure how.

How to init view with nib file? How to manage view controllers in a project?

I've created new Single View project in Xcode. In ViewController.xib I've added new custom view inside another view. (grey rectangle)
Then I've created new files: "CoordinateSystemViewController".
Now, I want to init my custom view with CoordinateSystemViewController.xib.
I've tried (ViewController.m file):
- (void)viewDidLoad
{
[super viewDidLoad];
CoordinateSystemViewController *coordinateSystemViewController = [[CoordinateSystemViewController alloc] initWithNibName:#"CoordinateSystemViewController" bundle:nil];
coordinateSystemView = coordinateSystemViewController.view;
}
but it seems to be the wrong way because it doesn't work :P
Should I always create ViewController for each file?
Should every view be loaded from separate .xib file?
Is it a good habit to create one View Controller class with few views' IBOutlets which manages them all?
Inorder to use another UIView inside Controller , We do not need to use another controller for it. Instead manage UIView inside same Controller.
Refer to this for more Information.

reference between app controller and window controllers

at the risk of a public flogging, I was hoping someone could clarify my understanding of communicating between the app controller and a window controller. Here is the scenario using xcode4.
using the default AppDelegate.h and .m as the "controller" (which is also a delegate to MainMenu.xib). Have an ivar called int counter.
Created page.xib and PageController.h and .m (subclass NSWindowController). Imported it into AppDelegate.m
Used IBAction to create and view the page object. Like this:
if (!page1) {
PageController *page1 = [[Page
if (!page1) {
page1 = [[PageControoer alloc] initWithWindowNibName:#"page"];
}
[page1 showWindow:sender];
So the new window pops and we can press buttons, etc. The code for the new window is all in PageController.h and .m. and things basically work.
That is the context, here is where I'm confused.
a) question: let's say I want to access that original ivar in AppDelegate.h called counter from PageController. Either retrieving or updating the variable. What approach would I take?
b) confirm: let's say I'm back in the AppDelegate and want to get access to a selector from page1. I believe I can do this as so: [page1 runaction]; or [[page1 variable] setStringValue:#"hello"];
(this complies but I'm not sure it really works because I can't get the changes into the xib view.)
ok and the stumper. Say another view was created with another view controller call it Page2Controller.h and .m.
c) how should data flow between page and page2 -> via the AppDelegate or directly? what would the syntax look like to connect them together?
I've been following tutorials, but they don't really cover this back and forth messaging. Thanks for all the help!
a) Generally, if you want to have data that is accessed by your controllers, it should be in a model which they are given access to in some way. You can access things in the app delegate using this method:
AppDelegate* appDelegate = [[NSApplication sharedApplication] delegate];
[appDelegate <some method>];
b) I don't understand what you're asking. If the app delegate has a pointer to page1, then yes, you can call it directly.
c) Again, you should have a data model of some sort. The controllers should update the data model when the user changes the view. You can use notifications, IBActions, and direct calls to do the updating, for example. You should look up the Model, View, Controller design pattern.

Cocoa Core data: cannot save Created Items in NSTableview

I'm am a beginner in mac os x development and am trying to get started with all this.
Here is my problem : I've create a non-document based cocoa app using core data as storage. I've added an entity and attributes to the xdatamodel. In IB i've created an NSArrayController and linked it properly. I've created an nstableview binded to the nsarraycontroller. Next I added a button linked to nsarraycontroller with the " add: " method.
When I try it out, I can add and edit the items in the table.
Here comes the problem: Core data is supposed to save everything automatically, but to make sure i linked the "save" button in the menu to the appdelegate and to the " file's owner" , first responder, application... everything possible ( with both " save :" and " saveaction:" methods ).
And still it doesn't save when clicking save: when I restart the cell created ( and renamed ) are gone.
And also, I didn't even edit the source code yet; core data for such simple tasks is supposed to only need Interface builder.
Please help me for this, I haven't found any threads resolving this problem.
Thank you in advance.
To save the changes, you'll have to call save: on the managed object context.
If you look at the CoreDataBooks example, you'll see how they call it like this as a result of the user pressing save:
- (IBAction)saveAction:(id)sender {
NSError *error;
if (![[self managedObjectContext] save:&error]) {
// Update to handle the error appropriately.
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort(); // Fail
}
}

Resources