Display text (string) in the storyboard - objective-c

I want to display some text (string) in a NSTextView Outlet in a storyboard file, but it does not work. I am not sure if I used the best approach. Is the selected method "insertText" the correct one? is the outlet connection correct? Does the storyboard support the NSTextView class?
Any help is much appreciated.
ViewController.h
#interface ViewController : NSViewController
#property (unsafe_unretained) IBOutlet NSTextView *closerText;
#end
ViewController.c
#implementation ViewController
#synthesize closerText;
- (void)viewDidLoad {
[super viewDidLoad];
[closerText insertText:#"Hello World"];
}
#end

make sure the NSTextView is connected to the closerText IBOutlet. insertText is the correct function.

Related

Objective-c - control outlet from other class

I just like to play with coding for a hobby, so probably a noob question;
I have a simple storyboard for MacOS with 2 views. Both have there own classes (main class and subclass). How can I control a outlet in the subclass from the main class?
for example
I have a button (IBAction) in the mainclass and a textfield (IBOutlet) in the subclass. I want to set the stringvalue for the textfield with a click on the button in main.
I have searched a lot last days but just don't get it. (or just need a push in the right direction)
EDIT after JingJingTao's answer:
I used the control-drag function to open the second window.
I tried the code JingJingTao gives, but the textfield doesn't respond to the action.
My classes look like this now:
ViewController.h
#import <Cocoa/Cocoa.h>
#interface ViewController : NSViewController
- (IBAction)newText:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "ViewController2.h"
#interface ViewController ()
#property (nonatomic) ViewController2 *subclass;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
}
- (void)newText:(id)sender {
self.subclass.textField.stringValue = #"button pressed";
}
#end
ViewController2.h
#import "ViewController.h"
#interface ViewController2 : ViewController
#property (nonatomic) IBOutlet NSTextField *textField;
#end
ViewController2.m
#import "ViewController2.h"
#interface ViewController2 ()
#end
#implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
}
#end
Update:
I've attached two screenshots of what it looks like in the storyboard for the first suggestion,
1) Add a view to your ViewController, set the class at the top right to 'YourView', 'YourView' is a just an NSView, add a textfield to it and hook it up.
2) Add YourView as a property to your ViewController, i.e. #property (nonatomic) IBOutlet NSView *yourView; and hook it up.
Let me know if there are any issues.
You just need to put the textfield in the public interface of your subclass, so you can access it in your main class, although it does sound like you're using inheritance and I don't think you need to but that's another topic :D.
Example:
In MainClassViewController.m
#interface MainClassViewController ()
#propert (nonatomic) Subclass *subclass;
#end
#implementation MainClassViewController
// I guess you already add your subclass to the main viewcontroller because they display on the same screen.
- (void)yourButtonTapMethod {
self.subclass.textfield.text = #"Your value";
}
In Subclass.h
#interface Subclass : NSObject
#property (nonatomic) IBOutlet UITextfield *textfield;
I use Cocoa Touch instead of Cocoa, so maybe it's NSTextfield for you. Please let me know if this does not answer your question, good luck.

Changing views on a window with a button click

what I'm basically trying to make is a very simple program that can switch back and forth between 2 views on a single window.
When the program loads there is a window with a custom view that contains a login button. When clicked, the view changes to a second custom view that contains a label and a logout button. I have this much working.
What I can't figure out is how to get the logout button to bring me back to the first view.
Here is the code for the AppDelegate class where i have the button method to switch the view:
header:
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (weak) IBOutlet NSWindow *window;
#property (weak) IBOutlet NSView *loginView;
- (IBAction)loginButtonClicked:(id)sender;
#end
implementation:
#import "AppDelegate.h"
#import "myCustomView.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
- (IBAction)loginButtonClicked:(id)sender {
[_loginView removeFromSuperview];
myCustomView *new = [[myCustomView alloc]initWithNibName:#"myCustomView" bundle:nil];
[[[self window]contentView]addSubview:[new view]];
}
#end
This is the code for my custom class that is a subclass of NSViewController.
header:
#import <Cocoa/Cocoa.h>
#class AppDelegate;
#interface myCustomView : NSViewController
#property (strong) IBOutlet NSView *logoutView;
- (IBAction)logoutButtonClicked:(id)sender;
#end
implementation:
#import "myCustomView.h"
#implementation myCustomView
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
}
- (IBAction)logoutButtonClicked:(id)sender {
[_logoutView removeFromSuperview];
myCustomView *newController = [[myCustomView alloc]initWithNibName:#"MainMenu" bundle:nil];
[[[self window]contentView]addSubView:[newController view]];
//this does not work. No visible #interface for 'myCustomClass' declares the selector 'window'
}
#end
The button method to go back to the login page is where I'm stuck. Even though I've added the header file for AppDelegate into myCustomClass I cannot use the instance of NSWindow. What am I doing wrong here? Am I at least on the right track? any help here is greatly appreciated.
I also tried using #class instead of #import, but still can't use the instance of NSWindow from AppDelegate.
Here are the pictures of my two xib files:
[][1
UPDATE: The suggestions from Paul Patterson in his comments were very helpful, but haven't solved my problem. For now what I am doing to get my project to work is putting the buttons in the window instead of the views and then hiding them when i don't need them. This works and I can switch back and forth, however I still can't figure out how to use a button on a custom view itself to load a different view onto the same window.

How can i make a UITabbar like this?

I would like to create a UITabbar like below but i don't know what is the logic to do that.
Here is the large answer:
First of all, you will need to create a UIView subclass to get a view that looks like the bar that you want. It can be composed by a background UIImageView and three buttons.
Then, the best thing would be to create a subclass of the UITabBarController and in its viewDidLoad or at any point where the flow will go through just once, you instantiate one view of type specified at first point. You should place the frame of this view in order to hide the original tabbar of the controller.
This would be the custom bar header file:
#interface CustomBar : UIView
{
}
#property (nonatomic, retain) UIImageView *backgroundView;
#property (nonatomic, retain) NSArray *buttons;
#end
You can easily complete the implementation. You can try to look for how to instantiate it with a nib file to make it easier to design it. In order to test, you can first just set the background color to green or something visible.
Then, this would be the subclass of the UITabBarController class:
#interface CustomTabBarController : UITabBarController
#property (nonatomic, retain) CustomBar *customBar;
#end
#implementation CustomTabBarController
- (void)viewDidLoad
{
[super viewDidLoad];
self.customBar = [[[CustomBar alloc] initWithFrame:[self.tabBar frame]] autorelease];
[self.view addSubview:self.customBar];
}
#end
Please, remember to implement the dealloc if you are not using ARC.
The thing I am not sorting out here is how to create the communication between the buttons from the custombar and the tabbarcontroller. This should be solved by delegates. If you need help with that, I will complete that too.
Good luck!

Change ViewControllers & Pass Data in iOS

I'm trying to have my app take a picture then pass the image onto another view for editing but I can't seem to figure out how to change views, how to add an "ID" to a view in the storyboard or how to pass data between views.
The communication between two UIViewControllers needs to be managed manually, however, if you are using storyboards to create your app, there's some things that you need to take into account.
Let's say you have FirstViewController and SecondViewController(Lets assume you have everything set up in your Storyboard). FirstViewController will pass a UIImage to SecondViewController and they will look something like this.
#interface FirstViewController : UIViewController
- (IBAction)transitionToNextViewController;
#property (retain, nonatomic) UIImage *image;
#end
#implementation FirstViewContoller
- (IBAction)transitionToNextViewController;
{
[self performSegueWithIdentifier:#"SegueIdentifier"];
}
#end
And:
#interface SecondViewController : UIViewController
#property (retain, nonatomic) UIImage *image;
#end
You are probably wondering how are you supposed to pass the image to the SecondViewController. Well, when using storyboards, your UIViewControllers will receive a call to their method prepareForSegue:sender: . All you have to do is set the image property for the second UIViewController there.
#implementation FirstViewController
- (IBAction)transitionToNextViewController;
{
[self performSegueWithIdentifier:#"SegueIdentifier"];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
SecondViewController *secondViewController = (SecondViewController *)segue.destinationViewController; // You have to cast it
secondViewController.image = self.image;
}
#end
and that's it. To better understand Storyboards please read the apple docs here.

Obejct-C / Xcode Not able to add/change text in UITextView

For some reason, i'm not able to change or add text in my UITextView from code.
I have made Outlet and connected (All in IB) - nothing happens.
Even tried to add -setNeedsDisplay
Do i need to set some property ? - This i driving me nuts....
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextViewDelegate> {
UITextView *textInfoView;
}
#property (nonatomic, retain) IBOutlet UITextView *textInfoView;
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
textInfoView.text = #"Test";
}
-(void)viewDidAppear:(BOOL)animated{
textInfoView.text = #"Test";
textInfoView.text = [textInfoView.text stringByAppendingString:#"Line2"];
[textInfoView setNeedsDisplay];
It seems, really strange that this code doesn't update the text inside the UITextView. I have tried and it works for me ... I think that you don't have correctly linked the Outlet in interface builder, or you don't have associated your view controller to your view ...
UPS - Got it...
Forgot to #synthesize...
For some reason i did not get warning or "setter error" in Xcode 4.4, maybe it's a version bug ?