Create a common outlet variable in superclass - iphone

Is it possible to define an outlet in the parent class that child classes can override?
For example, I have the following views:
#interface ParentView
#view
#interface FirstChildView : ParentView
#view
#interface SecondChildView : ParentView
#view
and the following controllers:
#interface ParentController
// ???
#end
#interface FirstChildController : ParentController
#property (strong, nonatomic) IBOutlet FirstChildView *myView;
#end
#interface SecondChildController : ParentController
#property (strong, nonatomic) IBOutlet SecondChildView *myView;
#end
Is it possible to define an outlet in the ParentController that acts as a "parent" outlet for both myViews under FirstChildController and SecondChildController?

But why are you doing this... you can do it like below.
#interface ParentController
#property (strong, nonatomic) IBOutlet ParentView *myView;
#end
#interface FirstChildController : ParentController
//Assign it from your code/outlet for First Child view.
//Like self.myView = <FirstChildView>
#end
#interface SecondChildController : ParentController
//Assign it from your code/outlet for Second Child view.
//Like self.myView = <SecondChildView>
#end
and you can use subclass properties by typecasting.

Thank you all for responding to my question. The following is what I ended up going with:
#interface ParentController
#property (strong, nonatomic) UIView *myView;
#end
#interface FirstChildController : ParentController
- (UIView *) myView
{
return self.myFirstChildView;
}
#property (strong, nonatomic) IBOutlet FirstChildView *myFirstChildView;
#end
#interface SecondChildController : ParentController
- (UIView *) myView
{
return self.mySecondChildView;
}
#property (strong, nonatomic) IBOutlet SecondChildView *mySecondChildView;
#end
Therefore, if I have a scene using FirstChildController, the 'self.myView' in ParentController will refer to 'myFirstChildView'.

Related

How to make update method to be called in SpriteKit?

My ViewController.h:
#import UIKit;
#import SpriteKit;
#interface BGGameViewController : UIViewController
#property (nonatomic, weak) IBOutlet SKView *skView;
#property (nonatomic, weak) IBOutlet UIButton *finishGameButton;
#property (nonatomic, weak) IBOutlet UIButton *startNewGameButton;
- (IBAction)finishGame:(id)sender;
- (IBAction)startNewGame:(id)sender;
#end
But I don't know how to make update method to be called.
The method is this...
- (void)update:(NSTimeInterval)currentTime
{
...
}
This needs to be in your SKScene class though, not your viewcontroller.

How to subclass UITextView

How can I subclass UITextView in UIActionSheet to overwrite canBeforeFirstResponder to disable copy, select, and select all in UITextView.
#import <UIKit/UIKit.h>
#class English;
#protocol EnglishDelegate
- (void)dismissViewDidFinish:(UIViewController *)viewController;
#end
#interface English: UIViewController <UITextViewDelegate, UIScrollViewDelegate>
{
id<EnglishDelegate> delegate;
UITextView *textView;
UINavigationBar *navBar;
UINavigationController *navigationController;
}
#property (nonatomic, retain) UITextView *textView;
#property (nonatomic, assign) UINavigationBar *navBar;
#property (strong, nonatomic) UINavigationController *navigationController;
#property (nonatomic, assign) id<EnglishDelegate> delegate;
#property (nonatomic, retain) UIScrollView *scrollView;
-(void)dismissView:(id)sender;
#end
Anyone knows how to subclass it in h file.
Thanks for help.
You wouldn't subclass UITextView inside of another class; you would just plain subclass it in another file and override the canBecomeFirstResponder as so:
- (BOOL)canBecomeFirstResponder {
return NO;
}

Calling a function in viewcontroller from appdelegate

I want to call a function in a viewController from my appDelegate but with the following code, it doesn't get called. What am I doing wrong?
AppDelegate.h:
#import <UIKit/UIKit.h>
#import "DetailsToTransfer.h"
#class AccountDetailTransferViewController;
#interface AccountDetailTransferAppDelegate : NSObject <UIApplicationDelegate> {
DetailsToTransfer *objDetailsToTransfer;
}
#property (nonatomic, retain) DetailsToTransfer *objDetailsToTransfer;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet AccountDetailTransferViewController *viewController;
-(void)sendToTransferScreen:(NSArray *)detailsArray;
#end
AppDelegate.m:
....
-(void)sendToTransferScreen:(NSArray *)detailsArray {
[objDetailsToTransfer setLabels:detailsArray];
objDetailsToTransfer = [[DetailsToTransfer alloc]initWithNibName:#"DetailsToTransfer" bundle:nil];
[self.window addSubview:objDetailsToTransfer.view];
}
DetailsToTransfer.h:
#import <UIKit/UIKit.h>
#interface DetailsToTransfer : UIViewController {
NSArray *accountDetailsArray;
UILabel *nameLabel;
UILabel *accountLabel;
IBOutlet UIButton *btnTransfer;
IBOutlet UIButton *btnBack;
}
#property (nonatomic, retain) NSArray *accountDetailsArray;
#property (nonatomic, retain) IBOutlet UILabel *nameLabel;
#property (nonatomic, retain) IBOutlet UILabel *accountLabel;
-(IBAction)sendDetails;
-(IBAction)goBack;
-(void)setLabels:(NSArray *)array;
#end
DetailsToTransfer.m
....
-(void)setLabels:(NSArray *)array {
NSLog(#"Setting Labels");
self.nameLabel.text = [array objectAtIndex:0];
self.accountLabel.text = [array objectAtIndex:1];
}
....
I would like to add that all the properties have been synthesized and that I'm calling the method in the appDelegate properly (i checked using NSLogs)
In AppDelegate.m:
Looks as if you are calling a method on your object before the object has been created. Until you have alloc / init your object, there will be no labels to set text.
Try changing your method to this:
-(void)sendToTransferScreen:(NSArray *)detailsArray {
if (!objDetailsToTransfer) {
objDetailsToTransfer = [[DetailsToTransfer alloc]initWithNibName:#"DetailsToTransfer" bundle:nil];
[objDetailsToTransfer setLabels:detailsArray];
[self.window addSubview:objDetailsToTransfer.view];
}
}
The problem might be that you are trying to set the values on a object that hasn't been created yet. I also provided an if statement to prevent the object from being created multiple times.

Hiding UI Button After Pressed

Hey!
I am trying to hide a UIButton after it is pressed
This is my .h code:
#import <UIKit/UIKit.h>
#interface TemplateTestViewController : UIViewController { }
- (IBAction)ButtonPressed;
IBOutlet UIButton *sample; #property (nonatomic, retain) IBOutlet UIButton
*sample;
#end
This is my .m code:
#import "TemplateTestViewController.h"
#import "Page.h"
#implementation TemplateTestViewController
#synthesize sample;
-(IBAction) ButtonPressed{
if ( ([sample.selected=YES]))
{
sample.hidden = YES;
}
I get four errors and the code does not work. What am I doing wrong? I am new so some sample code would be much appreciated!
you have issue in the your .h file with #interface declaration. use below
#import <UIKit/UIKit.h>
#interface TemplateTestViewController : UIViewController {
IBOutlet UIButton *sample;
}
#property (nonatomic, retain) IBOutlet UIButton *sample;
- (IBAction)ButtonPressed;
#end
And in your .m class
-(IBAction) ButtonPressed {
if(sample.selected == YES)
{
sample.hidden = YES;
}
}

Class Delegate does not implement protocol

I`m doing something wrong here but I can't figure out what it is .
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate> {
UIWindow *window;
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
UIView *flipside;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
#property (nonatomic, retain) IBOutlet UIView *flipside;
#property (nonatomic, retain) NSMutableArray *viewControllers;
- (IBAction)showInfo:(id)sender;
- (IBAction)changePage:(id)sender;
#end
AppDelegate.m
- (IBAction)showInfo:(id)sender {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
This is where I`m getting :
warning: class 'AppDelegate' does not implement the 'FlipsideViewControllerDelegate' protocol.
After line :
controller.delegate = self;
My FlipsideViewController.h looks like this :
#import <UIKit/UIKit.h>
#protocol FlipsideViewControllerDelegate;
#interface FlipsideViewController : UIViewController {
id <FlipsideViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
- (IBAction)done:(id)sender;
#end
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#en
Any help would be greatly appreciated :)
It's exactly what the error message says. AppDelegate just doesn't implement the protocol. In your header file, add FlipsideViewControllerDelegate between the brackets (i.e. <UIApplicationDelegate, UIScrollViewDelegate, FlipsideViewControllerDelegate>), and implement the -flipsideViewControllerDidFinish: method.
try adding FlipsideViewControllerDelegate to the appDelegate
#interface AppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate,FlipsideViewControllerDelegate> {
UIWindow *window;
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
UIView *flipside;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}