Customized init method on an object to initWithWindowNibName an NSWindowController property - objective-c

What I'm trying to do is create a method to an object which opens a window.
In this window I want to output some properties of the object's instance.
To do this I created a "Profile" subclass of NSObject, which has an NSWindowController property called "view".
#interface Profile : NSObject {
\\...
}
#property (readwrite, assign) NSWindowController *view;
Since I cannot connect "view" to the window with Interface Builder (or at least I don't know how) I have to do so with the "initWithWindowNibName". So I tried overriding the "Profile"'s init method like this:
-(Profile *)init{
self = [super init];
if(self){
[[self view] initWithWindowNibName:#"Profile"];
}
return self;
}
I don't know whether my approach is correct, fact is when I try showing the window it doesn't appear. Here's how I tried:
Profile *profile = [[Profile alloc] init];
[[profile view] showWindow:self];
Hope you can help :)

Don't you want something like:
#interface Profile:NSObject
#property (nonatomic, strong) NSWindowController *windowController;
#end
and:
- (Profile *)init {
self = [super init];
if( !self ) { return nil; }
self.windowController = [[NSWindowController alloc] initWithWindowNibName:#"Profile"];
return self;
}
and:
// show window
Profile *profile = [[Profile alloc] init];
[[profile windowController] showWindow:self];
(I'm assuming ARC.)
EDIT:
For clarity to the OP, I followed the his property nomenclature, which was to name the NSWindowController property view. It is confusing, though because a NSWindowController is not a view. For clarity to others, I've changed it.

Related

Modifying string content in NSTextView works under viewDidLoad method, but not under myMethod

I am trying to update the contents of an NSTextView that is connected to myViewController as a referencing outlet to the Files Owner which is the subclass myViewController.
When I use an IBAction from a button, or use the viewDidLoad method of the controller, I can update the text fine. However, when I try run the method from another class (referred to in this example as anotherViewController), it runs the method, but the textview does not change.
myViewController.h:
#import <Cocoa/Cocoa.h>
#import "anotherViewController.h"
#interface myViewController : NSViewController { }
#property (unsafe_unretained) IBOutlet NSTextView *outText;
#property (weak) IBOutlet NSButton *updateMeButton;
- (void)updateTextView:(NSString *)argText;
- (void)updateTextViewWithoutArg;
#end
myViewController.m:
#import "myViewController.h"
#interface myViewController ()
#end
#implementation myViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.outText.string = #"I work successfully";
}
- (IBAction)updateMeButton:(id)sender {
self.outText.string = #"I am updated text! I also work!";
}
- (void)updateTextView:(NSString *)argText {
self.outText.string = #"I don't make it to the NSTextView :(";
NSLog(#"Should have updated text view");
}
- (void)updateTextViewWithoutArg {
self.outText.string = #"I don't make it to the NSTextView :(";
NSLog(#"Should have updated text view");
}
#end
In anotherViewController.m , which has all the relevant imports, I call this:
myViewController *viewtask = [[myViewController alloc] init];
[viewtask updateTextViewWithoutArg];
Nothing happens. The method runs and logs that it should have updated, but no text updates. I have tried many different approaches, including textstorage and scrollrange methods, they all work the already working sections, but make no difference in the sections not working.
I've also tried just for fun:
myViewController *viewtask;
[viewtask updateTextViewWithoutArg];
Also using the instance variable _outText
Also using [self.outText setString:#"string"];
Also using [_outText setString:#"string"];
Again, they work but only in the already working sections.
This should be simple but isn't logical to me. In swift all I need to do is
self.outText.string = "I update whenever I'm called!"
Views you create in Interface Builder are lazily created, so if you access them before viewDidLoad is called they are nil.
If your case, calling
myViewController *viewtask = [[myViewController alloc] init];
does not cause the views to be created so when you call
[viewtask updateTextViewWithoutArg];
self.outText is nil.
You can see that this is what is happening by updating your code as below:
- (void)updateTextView:(NSString *)argText {
NSAssert(self.outText != nil, #"self.outText must not be nil");
self.outText.string = #"I don't make it to the NSTextView :(";
NSLog(#"Should have updated text view");
}
you should see the assert fire.
I appear to have found a solution by making myViewController a singleton class and using sharedInstance. For this particlar app, myViewController is a debug output window and will never need to be placed in another view.
I won't accept this answer yet, as it's not the best one I'm sure. There may still be a proper solution presented that allows finding the applicable myViewController instance, and modifying the outText property attached to it. Using this singleton makes subclassing tedious as I would have to make a new class for every instance if I wanted to be able to address say 10 View Controllers.
Anyway - the way I've been able to satisfy my simple requirement:
myViewController.h:
#import <Cocoa/Cocoa.h>
#import "anotherViewController.h"
#interface myViewController : NSViewController { }
#property (unsafe_unretained) IBOutlet NSTextView *outText;
#property (weak) IBOutlet NSButton *updateMeButton;
- (void)updateTextView:(NSString *)argText;
- (void)updateTextViewWithoutArg;
+ (id)sharedInstance;
#end
myViewController.m:
#import "myViewController.h"
#interface myViewController ()
#end
#implementation myViewController
static myViewController *sharedInstance = nil;
+ (myViewController *)sharedInstance {
static myViewController *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[myViewController alloc] init];
});
return sharedInstance;
}
- (void)viewDidLoad {
[super viewDidLoad];
sharedInstance = self;
}
- (void)viewDidUnload {
sharedInstance = nil;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.outText.string = #"I work successfully";
}
- (IBAction)updateMeButton:(id)sender {
sharedInstance.outText.string = #"Button Pressed";
}
- (void)updateTextView:(NSString *)argText {
sharedInstance.outText.string = argText;
}
- (void)updateTextViewWithoutArg {
sharedInstance.outText.string = #"I make it to the TextView now";
}
#end
Now when I use this code from within anotherViewController.m it updates the right instance:
[myViewController.sharedInstance updateTextView:#"Updating with this string"];

How to access NSViews from other classes

Before I start, I am a beginner, so please don't overwhelm me. (even-though I probably did with you sorry) :/
So basically I have 1 main view (NSView) onto which I am loading other views. So my awakeFromNib method works and it loads my start menu view (main menu to a tic tac toe game). On the menu is a single player and a double player button, and depending on which button I click, I would like the program to load further views.
So I have an AppController class which controls which views are loaded through a setViewController method. The issue is that I have not found a way to connect the buttons from my start menu view to the to the AppController class. So I thought if inside the start menu class I create an object of type AppController and then call on the setViewController method when the single or double player button is pressed, it would change the views accordingly, but it turns out it does nothing. However when I call on the setViewController method inside the AppController class, it does work. So I think the issue has to be somewhere with accessing the view from outside its class, but I might be wrong. Any help would be greatly appreciated, I have spent a lot of time trying to figure this out and I have not had any luck with anything I tried. Here is my AppController class and my start
screen class.
AppController.h:
#interface AppController : NSObject
#property (weak) IBOutlet NSView *mainMenu;
#property (strong) NSViewController *mainViewController;
-(void)setViewController:(NSInteger)viewNumber;
#end
AppController.m:
#implementation AppController
#synthesize mainMenu = _mainMenu;
#synthesize mainViewController =_mainViewController;
NSString *const kStartScreen = #"StartScreenViewController";
NSString *const kOnePlayerMenu = #"OnePlayerMenuViewController";
NSString *const kTwoPlayerMenu = #"TwoPlayerMenuViewController";
int test = 0;
enum{
kStartScreenView = 0,
kOnePlayerView,
kTwoPlayerView
};
-(void)awakeFromNib
{
[self setViewController:0];
}
-(void)setViewController:(NSInteger)viewNumber
{
[[_mainViewController view] removeFromSuperview];
if(viewNumber==kStartScreenView)
{
self.mainViewController = [[StartScreenViewController alloc] initWithNibName:
kStartScreen bundle:nil];
}
else if(viewNumber==kOnePlayerView)
{
self.mainViewController = [[OnePlayerMenuViewController alloc] initWithNibName:
kOnePlayerMenu bundle:nil];
}
else if(viewNumber==kTwoPlayerView)
{
self.mainViewController = [[TwoPlayerMenuViewController alloc] initWithNibName:
kTwoPlayerMenu bundle:nil];
}
[_mainMenu addSubview:[_mainViewController view]];
[[_mainViewController view] setFrame:[_mainMenu bounds]];
}
#end
StartScreenViewController.h:
#interface StartScreenViewController : NSViewController
- (IBAction)OnePlayer:(id)sender;
- (IBAction)TwoPlayer:(id)sender;
#end
StartScreenViewController.m:
#interface StartScreenViewController ()
#end
#implementation StartScreenViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (IBAction)OnePlayer:(id)sender
{
AppController *appControllerObj = [[AppController alloc] init];
[appControllerObj setViewController:1];
}
- (IBAction)TwoPlayer:(id)sender
{
AppController *appControllerObj = [[AppController alloc] init];
[appControllerObj setViewController:2];
}
#end
Actually, I figuered it out, all you have to do is make a class method in the AppController which stores the used object (self) as a class variable. Then you can access that object from anywhere. It might not be the most efficient way of doing it, I have no idea as I am just a beginner, but it worked for me! :D
Here's relevant the code:
-(void)awakeFromNib
{
[self setViewController:kStartScreenView];
viewObject = self;
}
+(id)getViewObject
{
return viewObject;
}
viewObject is defined as a class variable of type id.

UIViewController Retaining in ARC

I have a subclass of UIViewController -> MyPopUpViewController
#protocol MyPopUpViewController Delegate;
#interface MyPopUpViewController : UIViewController
{
}
#property (nonatomic, strong) id <MyPopUpViewControllerDelegate> delegate;
-(IBAction) buttonPressed:(id)sender;
#end
#protocol MyPopUpViewControllerDelegate
-(void) popupButtonPressed: (MyPopUpViewController*)controller;
#end
I cannot have this MyPopUpViewController as an instance variable because this comes externally, and there could be many and multiple of these popups can be up. So far I tried this, and it crashes on the delegate call due to not being retained:
MyMainViewController:
-(void)externalNotificationReceived: (NSString*) sentMessage
{
MyPopUpViewController *popupView = [[MyPopUpViewController alloc] init];
popupView.delegate = self;
[self.view addSubview:popupView.view];
[popupView setInfo :sentMessage :#"View" :#"Okay"];
popupView.view.frame = CGRectMake(0, -568, 320, 568);
popupView.view.center = self.view.center;
}
-(void)popupButtonPressed:(MyPopUpViewController *)controller :(int)sentButtonNumber
{
NSLog(#"Popup Delegate Called");
[controller.view removeFromSuperview];
controller.delegate = nil;
controller = nil;
}
Once the popup comes up, and when the ok button is tapped, it crashes and never gets to that NSLog. How can I change
MyPopUpViewController *popupView = [[MyPopUpViewController alloc] init];
..so it would retain without making it an instance variable?
Thanks in advance.
You should be doing proper view controller containment by calling addChildViewController:.
- (void)externalNotificationReceived: (NSString*) sentMessage {
MyPopUpViewController *popupView = [[MyPopUpViewController alloc] init];
popupView.delegate = self;
[popupView setInfo :sentMessage :#"View" :#"Okay"];
popupView.view.frame = CGRectMake(0, -568, 320, 568);
popupView.view.center = self.view.center;
[self addChildViewController:popupView];
[self.view addSubview:popupView.view];
[popupView didMoveToParentViewController:self];
}
This will keep a proper reference to the view controller as well as properly pass various view controller events. Read about this in the docs for UIViewController and the "View Controller Programming Guide for iOS".
BTW - you should name your methods better. Example:
popupButtonPressed::
should be named:
popupButtonPressed:buttonNumber:
Usually delegates are weak-referenced instead of strong. I, myself, would name it something else as to not confuse other people.
Also, the following bit of code will have no effect:
-(void)popupButtonPressed:(MyPopUpViewController *)controller :(int)sentButtonNumber
{
...
controller = nil;
}
the controller would be released (set to nil) automatically at the end of the scope.

Objective C - iOS - UIWebView getting retained in ARC but Delegate methods are not being called

I am having an issue with ARC. It is not retaining the webview. The scenario is I have to send a webview from one viewcontroller to another one. The reason is when the user searches for something I want to take him to a new screen with some other options. (I have to use the same webview)
Here is the sample code: I have a ViewController1 which has a webview (I added it in the xib.) I am loading say google in it and once the user searches for something and when its done loading I have to take him to a new view controller and show the same webview in the new viewcontroller.
//ViewController1
#interface ViewController1 : UIViewController <UIWebViewDelegate>
#property (nonatomic, retain) UIWebView* testWebView;
#end
#implementation ViewController1
#synthesize testWebView;
- (void)viewDidLoad
{
[super viewDidLoad];
testWebView = [[UIWebView alloc]init];
testWebView.delegate = self;
[testWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.google.com"]]];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *html = [testWebView stringByEvaluatingJavaScriptFromString:
#"document.body.innerHTML"];
if ([self.testWebView.request.url.absoluteString rangeOfString:#"output=search"].location != NSNotFound) {
ViewController2* newViewController = [[ViewController2 alloc] init];
[newViewController setTestWebView:self.testWebView];
[self.navigationController setViewControllers:[NSArray arrayWithObject:newViewController] animated:NO];
}
}
- (void)dealloc{
[self.testWebView stopLoading];
self.testWebView.delegate = nil;
self.testWebView = nil;
}
In the second view controller I am loading stackoverflow.com after a delay of 10 secs. The problem is it is loading stackoverflow fine, but it is not calling any of the delegate methods. Why?
#interface ViewController2 : UIViewController <UIWebViewDelegate>
#property (nonatomic, retain) UIWebView* testWebView;
#end
#implementation ViewController2
#synthesize testWebView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.testWebView.delegate = self;
[self.view addSubview:testWebView];
[self performSelector:#selector(loadDifferentPage) withObject:nil afterDelay:10];
}
-(void)loadDifferentPage{
[self.testWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.stackoverflow.com/"]]];
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
NSLog(#"%s", __PRETTY_FUNCTION__);
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(#"%s", __PRETTY_FUNCTION__);
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(#"%s", __PRETTY_FUNCTION__);
return YES;
}
ViewController2 is retaining the webview but the delegate methods are not being called. Why?
Thanks
Sai
ViewController1 delloc method was causing the issue:
If I uncomment out self.textWebView.delegate = nil it works fine. The reason is first we are setting the webview for newViewController and later in dealloc of ViewController1 we are setting its delegate to nil.
- (void)dealloc{
[self.testWebView stopLoading];
if(self.testWebView.delegate == self)
self.testWebView.delegate = nil;
self.testWebView = nil;
}
First thing I noticed is you're not specifying the instance variable name when synthesizing a property. That's just asking for collisions. Here's an example of how that should look:
#interface ViewController1 : UIViewController <UIWebViewDelegate>
#property (nonatomic, strong) IBOutlet UIWebView* testWebView;
#end
#implementation ViewController1
#synthesize testWebView=_testWebView;
Also, I noticed in ViewController1 you used IBOutlet so everything is probably wired up in Interface Builder. Try making sure that you set the delegate property in Interface Bulider because you don't set it in the implementation. That would be why you're not receiving any messages.
ViewController2 looks like you set the delegate in code. The problem is, you DON'T have IBOutlet in front of the property. Normally this would mean that you simply setup the WebView in code, but in your example you do not ever create a new instance of a UIWebView control and assign it to self.testWebView. This means that if it does display on the page, it's because Interface Builder was used to create it. You couldn't set the delegate in code without using IBOutlet in front of the testWebView declaration so that's probably why it's not working in exmaple two.
#interface ViewController2 : UIViewController <UIWebViewDelegate>
#property (nonatomic, strong) UIWebView* testWebView; // Mising IBOutlet
#end
#implementation ViewController2
#synthesize testWebView;
- (void)viewDidLoad
{
[super viewDidLoad];
// missing any code that would create the webview [[UIWebView alloc] init]
self.testWebView.delegate = self; // You set the delegate in code here
[self.view addSubview:testWebView];
[self performSelector:#selector(loadDifferentPage) withObject:nil afterDelay:10];
}
Hope this helps, I'd have to see your full implementation to get more specific than this.

Array of NSViews in Cocoa?

I am working on a Mac application. One of the windows can load several NSView objects that are in the same NIB/XIB file.
But my code looks like this:
#interface TheWindowController : NSWindowController {
//Interface objects
IBOutlet NSTableView *detailsTree;
IBOutlet NSView *bigView;
IBOutlet NSView *subView1;
IBOutlet NSView *subView2;
IBOutlet NSView *subView3;
IBOutlet NSView *subView4;
IBOutlet NSView *subView5;
}
My question is if that is possible to hold all these IBOutlets inside an Array, Dictionary or something alike. So in the future I could do something like this in my implementation:
- (IBAction)traceTableViewClick:(id)sender {
//having now a NSArray called subviewsArray
[[[bigView subviews] objectAtIndex:0] removeFromSuperview];
[rightView addSubview: [subviewsArray objectAtIndex:[detailsTree selectedRow]]];
}
Is it possible? How? Any examples?
I have done something similar with a custom view that contains many controls that I want to manipulate en masse. You need to keep them separate in the #interface declaration so that the IBOutlet property works correctly with Interface Builder, but in your init method you can organize them into an array or NSArray yourself:
- (id)init
{
self = [super init];
if (self != nil)
{
_viewArray = [[NSArray alloc] initWithObjects:subView1, subView2,
subView3, subView4, subView5, nil];
}
return self;
}
- (void)dealloc
{
[_viewArray release];
...etc...
[super dealloc];
}
You can then work on them as you desire:
- (void)doThing
{
for (id view in _viewArray)
{
[view doSomething];
}
}
Just add your NSViews to a NS(Mutable)Array or a NS(Mutable)Dictionary the same way you would add any other object to them.