Accessing View from ViewController - objective-c

I created a ViewController in StoryBoard and linked it with the GamePanelViewController class.
Furthermore I linked the view of the ViewController with a second class called GamePanel which extends of UIView.
How can I access to the view which is created automatically by StoryBoard to perform some methods I implemented to the GamePanel class which is linked to the view by identity inspector?
GamePanel.h:
#import <UIKit/UIKit.h>
#import "Plattform.h"
#interface GamePanel : UIView
#property (strong, nonatomic) NSMutableArray *plattforms;
- (void)initGamePanel;
- (void)drawPlattforms:(CGContextRef)gc;
#end
GamePanel.m:
#import "GamePanel.h"
#implementation GamePanel
#synthesize plattforms;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)initGamePanel{
self.backgroundColor = [UIColor redColor];
self.plattforms = [NSMutableArray new];
// Initialization code
for(int i = 0;i<8;i++){
for(int j = 0;j<6;j++){
[self.plattforms addObject:[[Plattform alloc] initWithXCord:(14+j*37) andYCoord:(58+14+i*37)]];
NSLog(#"Init");
}
}
}

If you have actually linked the viewController's view property with your custom view, then you can simply use self.view within you viewController to access it.
If you have just dragged a UIView onto your viewController in your storyboard (and not linked it to the viewController's view property), you will need to create an IBOutlet for it (control-drag from the view to your interface, and give it a name), then you can reference it with the name you give it e.g. self.myView.

Related

How to access property from another class?

I have a newbie question. How do you access a property from another class?
In my program, I have a view in which there are three levels available: easy, intermediate and difficult. I have three buttons with tags (I'm trying stuff, so I've called those buttons: try1, try2 and try3) and I have created an int that holds the tag of the button pressed. So, the h of my first file:
#property (strong, nonatomic) IBOutlet UIButton *try3;
#property (strong, nonatomic) IBOutlet UIButton *try2;
#property (strong, nonatomic) IBOutlet UIButton *try1;
#property int level;
-(IBAction)buttonClicked:(id)sender;
the method in the m file:
- (IBAction)buttonClicked:(id)sender {
self.level = [sender tag];
NSLog(#"tag is %d", self.level);
}
Now, in my other view controller, I want to access the property "level" to change something, like so:
if (level == 0) { do somtething
}
if (level == 1) { do somtething
}
etc...
Assuming this second view controller was presented by the first view controller, rather than trying to access the property of the first one, the second one should have its own property for level and the first view controller should populate that second view controller's property when it presents the second view controller.
If you search Stack Overflow for "pass data between view controllers" (or variations thereof), you'll find good examples. It depends a bit upon how the second view controller is presented (e.g. if you used a segue, you set the second view controller's property in the first view controller's prepareForSegue).
You need to implement a custom init for your embedded viewController, for instance:
EmbeddedViewController.h
#import <UIKit/UIKit.h>
#interface EmbeddedViewController : UIViewController
- (id)initWithLevel:(int)level;
#end
EmbeddedViewController.m
#import "EmbeddedViewController.h"
#interface EmbeddedViewController ()
#end
#implementation EmbeddedViewController {
int selectedLevel;
}
- (id)initWithLevel:(int)level {
if (self = [super init]) {
selectedLevel = level;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You will have to use this custom init from your firstViewController:
- (IBAction)buttonClicked:(id)sender {
self.level = [sender tag];
EmbeddedViewController *evc = [[EmbeddedViewController alloc] initWithLevel:[sender tag]];
[self.navigationController pushViewController:evc animated:YES];
}

Cocoa app doesn't show textview

I'm an iOS developer and I want to create a simple desktop app. I thought the switch would go perfect but it doesn't.
I've created a cocoa app ( from the xCode template ). Now I don't want to use user interface builders and stuff so I wrote my first controller like this:
#interface MainViewController ()
#property (nonatomic, strong) NSTextView *test;
#end
#implementation MainViewController
-(instancetype) init
{
self = [super init];
if(self)
{
NSLog(#"%s", __PRETTY_FUNCTION__);
_test = [[NSTextView alloc] init];
[_test setString:#"DKDDK"];
[self.view addSubview:_test];
[_test mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
}
return self;
}
#interface MainViewController : NSViewController
#end
And I just use the NSWindow that is created by the template:
#interface AppDelegate ()
#property (weak) IBOutlet NSWindow *window;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
MainViewController * mainView = [[MainViewController alloc] init];
[self.window.contentView addSubview:mainView.view];
mainView.view.frame = ((NSView*)self.window.contentView).bounds;
}
When I run the application it gives me:
[NSViewController loadView] loaded the "(null)" nib but no view was set.
I don't know how to solve this. How can I create an app without nib, just like you do on iOS?
If you aren't loading the view from a NIB then there is little need for a view controller.
Discard the view controller and subclass NSView instead, and set that as the window's content view.
Note: you are making a rod for your own back by not using IB.

Using protocol to trigger a segue from a UIView inside a UIViewcontroller

CONFIGURATION:
-UIviewController embedded in Navigationcontroller.
-UIviewController has a UIscrollview as subview
-UIscrollview has some views where charts are created: each view containing a chart has its own .h and .m file and from this file I want trigger a segue to a tableview controller.
-A Tableviewcontroller was added in xcode and a segue from the UIviewController to the TableViewcontroller was created as well (Xcode)
-created a protocol in the UIView to have the segue pushed from there.
PROBLEM:
delegate always nil, segue method will never be called
UIVIEW .h file
#protocol UItoUIvcDelegate <NSObject>
-(void)triggerSegue;
#end
#interface CFfirstGraph : UIView <CPTPlotDataSource , CPTPieChartDelegate,UIActionSheetDelegate>
#property(weak, nonatomic) id <UItoUIvcDelegate> delegate;
#end
UIVIEW .m file (snippet)
-(void)pieChart:(CPTPieChart *)pieChart sliceWasSelectedAtRecordIndex:(NSUInteger)index
{
if (self.delegate == nil)
{
NSLog(#"nil");
}
[self.delegate triggerSegue];
}
UIVIEWCONTROLLER .h file
#import "CFfirstGraph.h"
#interface CFMainViewController : UIViewController <UItoUIvcDelegate, UITableViewDelegate, UITableViewDataSource>
#end
UIVIEWCONTROLLER .m file (snippet)
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.contentSize = CGSizeMake(320, 1000);
[self.view addSubview:self.scrollView];
CFfirstGraph *click =[[CFfirstGraph alloc]init];
click.delegate = self ;
}
-(void)triggerSegue
{
[self performSegueWithIdentifier:#"detailedData" sender:self];
NSLog(#"estoy aqui");
}
What am I doing wrong ? why the delegate is always nil ? I tried to add the method setDelegate but still no luck.
Thanks,
dom
Make CFfirstGraph as a strong property.
#property (strong, nonatomic) CFfirstGraph * click;
- (void)viewDidLoad
{
[super viewDidLoad];
self.click =[[CFfirstGraph alloc]init];
self.click.delegate = self ;
self.scrollView.contentSize = CGSizeMake(320, 1000);
[self.view addSubview:self.scrollView];
}
ok, after many hours of sweat I found the issue.
First...delegate = nil was not the main problem.
The real issue was the protocol method triggering the segue was never called.
If i create and initialize a CFfirstGraph object (or even property) it won't be related to the view created already in x-code, and this is the main issue.
On the other hand...if I "CTRL-drag" an outlet from the UIview to the CFMainViewController i will have a property that is exactly the one i need:
#interface CFMainViewController () <UIScrollViewDelegate>
#property (weak, nonatomic) IBOutlet CFfirstGraph *FirstGraph;
Then i assign the delegate to self (CFMainViewController) in the viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
self.FirstGraph.delegate = self ;
}
and the delegate method "triggerSegue" will be executed being called from the UIVIEW.
Best Regards,
dom

Switching between custom uitableviews from inside an uiviewcontroller

So I have a UIViewController class called SocialMainViewController. It has an instance of a TwitterViewController (which inherits from an UITableViewController) and an instance of a FacebookViewController (which also inherits from an UITableViewController).
#interface SocialMainViewController ()
#property (strong, nonatomic) TwitterViewController* twitterVC;
#property (strong, nonatomic) FacebookViewController* facebookVC;
...
#end
#implementation SocialMainViewController
#synthesize twitterVC = _twitterVC;
#synthesize facebookVC = _facebookVC;
..
- (void)viewDidLoad
{
[super viewDidLoad];
UIColor* color = [[UIColor alloc] initWithRed:0.2 green:0.2 blue:0.2 alpha:1.0];
[self.navigationController.navigationBar setTintColor:color];
// Do any additional setup after loading the view
self.facebookVC = [[FacebookViewController alloc] init];
self.facebookVC.view.frame = self.view.bounds;
[self.view addSubview:self.facebookVC.view];
}
So the Facebook view controller gets all the information from facebook and store it in an array. But the cellForRowAtIndexPath on that FacebookViewController class never gets called and hence only an empty table view is visible.
I'm actually trying to switch between the facebookviewcontroller and twitterviewcontroller tableviews. That is why I had the two inside the socialmainviewcontroller.

Connect NSImageView to view using IB?

I've only programmed on the iPhone so far, so Cocoa is sort of confusing in certain ways for me. Here's where I've hit a snag. I wanted my window so that the background was invisible, and without a title-bar. Something like this:
Here's how I'm doing it:
I set my window's class to a custom window, which I've created like this:
CustomWindow.h
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#interface CustomWindow : NSWindow {
#private
NSPoint initialLocation;
}
#property(assign)NSPoint initialLocation;
#end
CustomWindow.m
//trimmed to show important part
#import "CustomWindow.h"
#implementation CustomWindow
#synthesize initialLocation;
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {
// Removes the window title bar
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
if (self != nil) {
[self setAlphaValue:1.0];
[self setOpaque:NO];
}
return self;
}
#end
Now, in my .xib file for this window I've added a custom view onto the window. I've set the view class to a custom class I've created that inherits from NSView. Here's how I'm setting that up:
MainView.h
#import <Cocoa/Cocoa.h>
#interface MainView : NSView {
#private
//nothing to see here, add later
}
#end
MainView.m
//trimmed greatly again to show important part
#import "MainView.h"
#implementation MainView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)drawRect:(NSRect)rect {
// Clear the drawing rect.
[[NSColor clearColor] set];
NSRectFill([self frame]);
}
#end
So here's my question. I've added a NSImageView to my custom view (MainView) in Interface Builder. However, for some reason I can't figure out how to connect this image view to an instance variable in my custom view. They seem like they can't be connected like I normally would if I was creating an iPhone app. Any ideas how this would be done?
You connect objects created in your XIB in Mac OS X the same way you do for iOS programs. Just add an NSImageView property to your main view, mark it as an IBOutlet and connect it up.
For example,
In MainView.h create a property for your NSImageView and make it an IBOutlet:
#import <Cocoa/Cocoa.h>
#interface MainView : NSView {
NSImageView *imageView;
}
#property(retain) IBOutlet NSImageView *imageView;
#end
In interface builder, make sure the class for the custom view is set to MainView, to do this click on the File's Owner object in the custom view XIB and then select the identity option in the inspector and enter MainView as the class type.
Next, CTRL+click File's owner and drag the arrow to the NSImageView and select the imageView outlet.
That's all there is to it. You should be able to reference the image view from code now.