Objective C - XIB load in NSView - macos

I'm new to Objective C coding and the MVC concept gives me some pain. My goal is to develop a Mac Application. I was able to create a .xib for the main window. It starts well.
My main NSWindow has its main NSView. In this NSView, I put 3 subwiews (using Interface Builder). What I want is to change the content of the 2nd subview (by loading the content from another XIB file, called SubWindow1.xib) when I click on a button placed in the 1st subview.
I'm able to catch the click and tried some "xib loading" code but it doesn't work (the content never appears in my 2nd subview).
What I did is:
put the NSButton (on the 1st subview)
put a NSObject on Interface Builder (class renamed CtMainWindow, "Ct" for Controler)
link the IBAction (changeToSubWindow1) and IBOutlet (vSubView2 which is my 2nd subview)
create the CtMainWindow class, create a VwSubWindow1 class (which extends NSView)
In changeToSubWindow1(), I wrote:
VwSubWindow1 * vProfile = [[VwSubWindow1 alloc] initWithFrame:vSubView2.bounds];
[vProfile loadXib];
[vProfile.superview addSubview:vProfile];
The loadXib() function does:
NSNib * nib = [[NSNib alloc] initWithNibNamed:[self #"SubWindow1"] bundle:nil]; //#"SubWindow1" is the name of the expected loaded xib without .xib extention
[nib instantiateWithOwner:self topLevelObjects:nil];
And Voila. I was hoping that loading the sub xib in a view, and putting that view in my 2nd subview, it would appear.
I also tried to put only the following code in changeToSubWindow1()
VwSubWindow1 * vProfile = [[VwSubWindow1 alloc] initWithFrame:vSubView2.bounds];
[vProfile loadSubWindow];
The loadSubWindow() function does:
[NSBundle loadNibNamed:#"SubWindow1" owner:self];
Without more success.
I already read lots of threads from stackoverflow and tried lots of solutions but none of them worked for me.
Can anyone give me the hint I need in order to load and display the Xib in a subview ?
Thanks in advance.

you can try this method
you can put 3 xib files and 3 windows. connect each window
self.packages = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self.window1.contentView addSubview:self.vc.view];
self.packages.view.frame = ((NSView*)self.window1.contentView).bounds;
[window1 makeKeyAndOrderFront:NSApp];

Related

WebView - Failed to set (contentViewController) user defined inspected property on (NSWindow)

I create a new Xcode project from scratch and it compiles fine.
I add a label to the View Controller and this compiles / runs / shows fine.
I then drag in a WebView into the View Controller but get this message when I run the application:
Failed to set (contentViewController) user defined inspected property on (NSWindow): *** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (WebView)
What am I doing wrong?
Just Link Webkit.framework in general->Linked Framework and Libraries
Well first of all you need to define the Web view so it actually knows what todo all it is doing right now is trying to find what to run here, see a tutorial on youtube or such, this is the code i used for mine, just to set it up.
first you want to put the web view into the view controller and add a toolbar to the bottom of it.
add four bar button items onto the bar.
and put a fixed space in-between the two.
then you need to set the styles of them do this by going to attributes inspector> button type > choose style
choose the following for the button 1st one is rewind 2nd is forward 3rd one is refresh last one is Cancel.
then control click and drag the button to the web view it will give you the options for it like load rewind and refresh.
then go to your storyboard and make sure you have you main and header files connected do this by selected the view controller in the storyboard files, then
change the class to the view controller you want to hook up to.
then hit enter
go to your header file(.h) and insert the following code,
#property (nonatomic, strong) IBOutlet UIWebView *webView;
put it under the #interface with brackets but make sure that the code is not inside of the brackets.
then what you need to do is go to your main file(.m) and insert the following code like this
#interface WebViewController ()
#end
#implementation WebViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//custom initialization
}
return self;
}
- (void)viewDidLoad {
NSURL *url = [NSURL URLWithString:#"https://www.google.com"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url]; [_webView loadRequest:requestURL];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
accept obviously you need to replace the void or just past into the right places
Then build and Run your code use iOS simulator iPhone 6.
Hope this helps have a nice day!
You should be using the wrong property webFrame of WebView instead of mainFrame
if let url = NSBundle.mainBundle().URLForResource("index", withExtension: "html"){
editorWebView.mainFrame.loadRequest(NSURLRequest(URL: url))
}
I don't think you are doing anything wrong. Its just a bug

Print is printing entire window, not the View

I have a simple program, for making sure the print works.
-Subclassed NSObject for a Controller, "ViewController.
-Subclassed NSView, View
Added a custom view to the window, set class to View.
Made ViewController delegate for View.
Added Object in IB, set class to ViewController.
Added IBOutlet to Delegate section of ViewController, connected it to the the custom view on the window.
In View, have a simple [myString drawInRect: rect], where myString is defined in the init as #"Hi".
When I run, the program prints "Hi" in the view. When I click the "Print" menu item, the print preview shows the entire window.
More: I hade an earlier test program which had no print code, I ran it and the print preview showed only the view. I've got through both codes and cannot find a difference, so I am lost at why one is working and the other is not.
Do any of you know why the print preview would show the entire window instead of the view?
[EDIT]-----
I also created an extremely simple program to check and have the same issue.
Subclassed NSView, MainView
Added Custom View to Window, set class to MainView
Added [str drawInRect: dirtyRect withAttributes:nil];
(note; NSString *str = #"Hello";).
[More Information]-------
I added a print method as follows to the NSView object:
-(void)printPDF{
NSRect r = [self bounds];
[[NSPrintOperation printOperationWithView:self] runOperation];
[self dataWithPDFInsideRect:r];
}
Added a button to the window, linked it to an IBAction method in the ViewController:
-(IBAction)printToPDF:(id)sender{
[view printPDF];
}
In the ViewController I have:
IBOutlet View *view;
This works! So, why does the default "print" menu item print the entire Window?
By default, you don't have to write any printing code and the printing just works. But, it provides default behavior (aka, print the entire window).
If you want custom printing behavior, you'd have to write your own printing method.(like you did in your extremely simple program). And you can link the default print menu item to your own printing method.

Xcode MVC problem

I have created a baseview project in xcode4. I want to use a new xib file to instead of the old viewController.xib file. So I just change the old viewController.xib file's name. and I also change the MainWindows.xib, let the UIViewController direct to the new name one xib file.
All are ok, but the view are transparent in the MainWindows, like link the xib to UIViewController are not correct. I don't know..
My step are:
1. create a new xib file.
2. link the new xib file to MyUIViewController class
![I think link way is this,I don't know if is correct?]
Try other way:
If I use the xcode template to add sub UIViewController (include xib and class file), and add the code like:
testViewControllerViewController *Controller = [[testViewControllerViewController alloc] init];
self.viewController = Controller;
[Controller release];
//self.window.rootViewController = self.viewController;
[self.window addSubview:self.viewController.view];
[self.window makeKeyAndVisible];
The xib content are show correct.
If I change the name of xib file, the xib content are not show. Because I want to create the Class first, and use the old xib file.
Any body know how to resolve it: Create xib file and sub UIViewController separately.
If these are way to do this, There is a problem: I have two xib file and link to only one UIViewController, if I use the UIViewController, which xib is used?
So, I want to sure whether can we create the xib file and sub UIViewController separately?
Why we must set the Custom Class file to SUB UIViewController, show as the picture above.
I'm not sure if I understand the problem correctly, but are you asking why you must always add the Controller's view as a subview to the main window?
The iPhone hierarchy is quite simple: every App has a main window, with a controller. This controller has a View, most of the time.
So, to make your app do something, and show something you will need to add the Controlling logic (from the Controller) and the View that it's controlling to the main window, by calling
[self.window addSubview:[Controller View]];

NSWindowController object linkage in Interface builder

I created a NSWindow xib file that I want to open on click of a button in another window.
Now, to control the behavior of the NSWindow, I dragged an object from Library in xib and changed it to subclass of NSWindowController (i.e. ListingWindowController) that I defined in XCode.
Similarly I also created a subclass of NSViewController (i.e. ListingViewController) to manage the NSView inside the NSWindow. To do this, I dragged NSViewController from Library in xib and changed its class to ListingViewController.
#class ListingViewController;
#interface ListingWindowController : NSWindowController {
IBOutlet ListingViewController *listingVC;
}
#property (nonatomic, retain) IBOutlet ListingViewController *listingVC;
#end
I connected window and listingVC of my window controller in IB.
Now to invoke this window on click of a button in my launch (first) window, I create the window controller using initWithWindowNibName like this..
- (IBAction) pushConnect:(id)sender {
NSLog(#"Connect pushed.");
if (wc == nil) {
wc = [[ListingWindowController alloc] initWithWindowNibName:#"ListingWindow" owner:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(closeWindow:) name:NSWindowWillCloseNotification object:nil];
[wc showWindow:sender];
}
}
The problem is that despite all the bindings done in IB for the view controllers of upcoming window/view, the window and listingVC comes out to be (null), (null) even after the new window has loaded (below code).
- (void)windowDidLoad {
[super windowDidLoad];
NSLog(#"windowDidLoad = %#, %#", self.window, self.listingVC);
}
Please help why the connections are not working. I'm banging my head against this problem for quite a while now.
PS: I'm coming from iOS programming background. So, I'm assuming the Mac's window/view controller behave similar to iOS UIViewControllers.
TIA..
Note that:
wc = [[ListingWindowController alloc] initWithWindowNibName:#"ListingWindow" owner:self];
means that self (it’s not clear what self is from your question) is the owner of ListingWindow.nib. This means that self is the one who keeps outlets to objects in that nib file, and self is responsible for releasing the top-level objects in the nib file. This also means that you’re creating an instance of ListingWindowController in your code and another instance inside your nib file since you’ve dragged an object of class ListingWindowController onto the nib file.
This is not how it’s supposed to be.
In the vast majority of cases, a window (view) controller loads a nib file and becomes its owner. It has a window (view) outlet that must be linked to a top-level window (view) in the nib file. Being the nib file’s owner, it must have been created before the nib file is loaded.
In order to achieve this for your window controller, you need to set the file’s owner class to ListingWindowController. You must not drag an object cube and instantiate the window controller inside the nib file. The window controller is the owner of the nib file, so it must exist before the nib file is loaded. You must also link the window outlet in file’s owner to the top-level window object in the nib file so that the window controller is aware of what window it should manage.
Having done that, use:
wc = [[ListingWindowController alloc] initWithWindowNibName:#"ListingWindow"];
instead of:
wc = [[ListingWindowController alloc] initWithWindowNibName:#"ListingWindow" owner:self];
since wc is supposed to be the owner of the nib file.
View controllers work similarly. They’re created before loading the nib file, are responsible for loading a nib file that contains a view as a top-level object, are that nib file’s owner, and have a view outlet that must be linked to that top-level view.
It’s not clear from your question whether you have a separate nib file for the view. If you don’t, then using a subclass of NSViewController is not needed at all — you could use a subclass of NSObject instead. If you insist on using NSViewController to manage a view that’s not loaded from a separate nib file, then you should override -loadView so that you get a reference to the view by some means other than loading it from a nib file, and sending it -setView: so that it is aware of the view it’s supposed to be managing.
Recommended reading: Nib Files in the Resource Programming Guide, NSWindowController class reference, NSViewController class reference.

Uitableview within uiview

I have Uitable view within uiview with navigation bar , I want to add button to the navigation bar to edit the contents of uitableview
any suggestion how to do that please
I think that you want to place your UITableView controller within a UINavigationController (see the UINavigationController class reference for more information).
In your UIViewController for the UITableView, you need to override the methods found under the "Configuring a Navigation Interface" section of the Apple Developer Documentation.
Within this section of methods lies - (UIBarButtonItem *)editButtonItem, which you could override to provide the button for your editing purpose. Under the hood, the UINavigationController will call this method to get the edit button for its user interface. Using this technique ensures that your app stays consistant with the user expierence iOS users have come to love.
There is solution that I tried and worked for me. The solution is
in .h file
UIBarButtonItem *edit;
and set property line as
#property (nonatomic , retain) IBOutlet UIbarButtonItem *edit;
in .m file
in your action handler of the button add
[self.tableView setEditing:TRUE];
Open the .xib file of your view controller and add a UIBarButtonItem either to left or right side of the Navigation Bar and connect the respective IBOutlet to edit button and also the selector method.
Now in the delgate of tableView add the following method
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//sets the editing style for every row.
return UITableViewCellEditingStyleDelete;
}
Now you can play around with the UITableViewDelegate Methods and also the above methos to get the desired functionality.
Hope it works and do communicate if it does!!

Resources