How to change value in UISegmentedControl? - xcode

I have set up a segmented control in my project. I want to use it in my settings scene to control whether the orientation is landscape left or landscape right. I have in set up in my storyboard and here is my code in my SettingsViewController.h
#import <UIKit/UIKit.h>
#interface SettingsViewController : UIViewController
{
IBOutlet UISegmentedControl *orientation;
}
#property (nonatomic, retain) UISegmentedControl *orientation;
- (IBAction) setOrientation;
#end
and here is my code in SettingsViewController.m
#import "SettingsViewController.h"
#implementation SettingsViewController
#synthesize orientation;
- (IBAction)setOrientation
{
NSLog(#"index = %d\n", orientation.selectedSegmentIndex);
if (orientation.selectedSegmentIndex == 0) {
NSLog(#"left\n");
}
if (orientation.selectedSegmentIndex == 1) {
NSLog(#"right\n");
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
#end
I was wondering why the value isn't changing from 0 to 1 when I select the right side of the control. Also I was wondering if in my .h I need to declare IBOutlet because I dont use the segmented control as an outlet. I just use it to take in which side is selected and then use that to set the orientation of the app.

Try this. Hook it up in the Storyboard to the segmentedControl using valueChanged.
- (IBAction)segmentedControlChanged:(id)sender {
UISegmentedControl *segmentedC = (UISegmentedControl *)sender;
if (segmentedC.selectedSegmentIndex == 0) {
// NSLog(#"selectedSegmentIndex == 0");
[self selectFirstSegment];
}
}

in segment action set the orientation landscape or potriat..and in .m file end of bool orientation type method set
return yes;
in all .m files .

Related

Cocoa: NSToolBar with Custom Views. Animation issues

So I got a Window:
Window.xib
I got a WindowController too:
WindowController.h reads:
#import <Cocoa/Cocoa.h>
#interface MainWindowController : NSWindowController
{
IBOutlet NSView *firstView;
IBOutlet NSView *secondView;
IBOutlet NSView *thirdView;
int currentViewTag;
}
-(IBAction)switchView:(id)sender;
#end
And the WindowController.m reads:
#import "MainWindowController.h"
#interface MainWindowController ()
#end
#implementation MainWindowController
-(id)init
{
self = [super initWithWindowNibName:#"MainWindow"];
if (self){
// Initialization code here
}
return self;
}
//- (void)windowDidLoad {
// [super windowDidLoad];
//
// // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
//}
#pragma mark - Custom view drawing
-(NSRect)newFrameForNewContentView:(NSView *)view
{
NSWindow *window = [self window];
NSRect newFrameRect = [window frameRectForContentRect:[view frame]];
NSRect oldFrameRect = [window frame];
NSSize newSize = newFrameRect.size;
NSSize oldSize = oldFrameRect.size;
NSRect frame = [window frame];
frame.size = newSize;
frame.origin.y -= (newSize.height - oldSize.height);
return frame;
}
-(NSView *)viewForTag:(int)tag{
NSView *view = nil;
if (tag == 0) {
view = firstView;
} else if (tag == 1) {
view = secondView;
} else {
view = thirdView;
}
return view;
}
-(BOOL) validateToolbarItem:(NSToolbarItem *)item
{
if ([item tag] == currentViewTag) return NO;
else return YES;
}
-(void)awakeFromNib
{
[[self window] setContentSize:[firstView frame].size];
[[[self window] contentView]addSubview:firstView];
[[[self window] contentView]setWantsLayer:YES];
}
-(IBAction)switchView:(id)sender
{
int tag = [sender tag];
NSView *view = [self viewForTag:tag];
NSView *previousView = [self viewForTag:currentViewTag];
currentViewTag = tag;
NSRect newFrame = [self newFrameForNewContentView:view];
[NSAnimationContext beginGrouping];
if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask)
[[NSAnimationContext currentContext] setDuration:1.0];
[[[[self window]contentView]animator]replaceSubview:previousView with:view];
[[[self window]animator]setFrame:newFrame display:YES];
[NSAnimationContext endGrouping];
}
#end
The problem I have is that when i switch tabs in my app, the custom view (and there are three of different sizes) draw differently each time. Look at the screenshots, all of the numbers should be centre aligned but they are sometimes and others not. Can anyone see what my error is please?
I will also add that all of the actions have been correctly configured + the code works perfectly if the custom view size is the same all the time.
The view that works
The view that almost works
Again pointing out that in my .xib all of the numbers are aligned to 0x and 0y axis.
Appdelegate.h
#import <Cocoa/Cocoa.h>
#class MainWindowController;
#interface AppDelegate : NSObject <NSApplicationDelegate> {
MainWindowController *mainWindowController;
}
#end
Appdelegate.m
#interface AppDelegate ()
#property (nonatomic) IBOutlet NSWindow *window;
#end
#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
}
-(void)awakeFromNib
{
if(!mainWindowController){
mainWindowController = [[MainWindowController alloc]init];
}
[mainWindowController showWindow:nil];
}
#end
In Interface Builder, make sure to disable the "autoresizes subviews" checkbox of the default view of your window

NSContainerView with multiple child view controllers in Xcode 6

I'm trying to link two different view controllers in an Xcode 6 storyboard with a NSContainerView so that they can be switched conditionally. Unfortunately this tutorial here isn't of any help since things seem to have changed since in Xcode.
So I have two different view controllers and one of them is loaded into the container view by default but I want to be able to load the second view controller into the container view programmatically. Xcode 6 only allows to create embed Segues when I drag from one to the other so that's not much of a help.
Can somebody tell me how this is achieved with Xcode 6?
First, here's a sample GitHub project of the solution: click. I wasn't sure whether you wanted to swap the views or simply push the 2nd view onto a proverbial stack, so I went with a push/pop scheme. If you want to swap the views instead, you should be able to do that fairly easily by just skipping the stack storage.
Essentially, we have our "host" NSViewController that holds a Container View (CV) inside of it. This host doesn't actually manually manage the view controller that the CV is showing at the moment. The way this is done is through, well, a sort of nested view controller that then manages all the other view controllers that you're going to show/hide/push/pop/swap/etc. (Note: you might be able to remove the layering a bit, but in iOS terms, I'm treating the 'Sub View Controller Manager' in the storyboard screenshot sort of like a UINavigationController).
We also take advantage of some custom segues/segue animators in order to be able to do more work in the storyboard.
You just have to tell the content view manager view controller to manipulate its subviews in such a way that the old views that you want to pop "back" to are retained (in this case, using an NSMutableArray) and such that the new views have the right frame or have their constraints set up properly.
Here is a screenshot of the storyboard: Each segue you see on the storyboard of a custom type (looks like this -> { } ) is of type SegueBetweenEmbedded in the sample project. Buttons that push perform a segue, and buttons labeled 'Pop' perform dismissController: on the NSViewController (so that was done in the storyboard).
Here's some code (and there's a lot of it, so I suggest looking at the sample project instead):
ViewController.h
#import <Cocoa/Cocoa.h>
#import "ContentManagerViewController.h"
#class ContentManagerViewController;
#protocol ContentManagerViewControllerHolder <NSObject>
-(ContentManagerViewController*)retreiveContentManagerController;
#end
#interface ViewController : NSViewController <ContentManagerViewControllerHolder>
#end
ViewController.m
#import "ViewController.h"
#import "ContentManagerViewController.h"
#import "BackForwardViewController.h"
#interface ViewController ()
#property ContentManagerViewController *vcController;
-(IBAction)pushViewController:(id)sender;
-(IBAction)popViewController:(id)sender;
-(IBAction)popToRootViewController:(id)sender;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)prepareForSegue:(NSStoryboardSegue *)segue sender:(id)sender {
if ([[segue destinationController] class] == [ContentManagerViewController class]) {
self.vcController = segue.destinationController;
}
}
-(ContentManagerViewController*)retreiveContentManagerController {
return self.vcController;
}
-(IBAction)pushViewController:(id)sender {
// note: this works, but then pop is broken via dismissController: since it wasn't done with a segue.
// Better way is to rig up a manual segue and execute the segue.
//BackForwardViewController *viewController = [[NSStoryboard storyboardWithName:#"Main" bundle:nil] instantiateControllerWithIdentifier:#"BackForwardStoryboardID"];
//[self.vcController push:viewController];
[self performSegueWithIdentifier:#"CustomSegueToBackForward" sender:self];
}
-(IBAction)popViewController:(id)sender {
[self.vcController pop];
}
-(IBAction)popToRootViewController:(id)sender {
[self.vcController popToRoot];
}
#end
SegueBetweenEmbedded.h
#import <Cocoa/Cocoa.h>
#interface SegueBetweenEmbedded : NSStoryboardSegue
#end
SegueBetweenEmbedded.m (sorry not sorry for the nested class)
#import "SegueBetweenEmbedded.h"
#import "ContentManagerViewController.h"
#import "ViewController.h"
#interface SegueAnimator : NSObject <NSViewControllerPresentationAnimator>
- (void)animatePresentationOfViewController:(NSViewController *)viewController fromViewController:(NSViewController *)fromViewController;
- (void)animateDismissalOfViewController:(NSViewController *)viewController fromViewController:(NSViewController *)fromViewController;
#end
#implementation SegueAnimator
- (void)animatePresentationOfViewController:(NSViewController *)viewController fromViewController:(NSViewController *)fromViewController {
NSViewController *parent = [fromViewController parentViewController];
if (parent && [parent class] == [ContentManagerViewController class]) {
ContentManagerViewController *manager = (ContentManagerViewController*)parent;
[manager push:viewController];
}
else if ([fromViewController conformsToProtocol:#protocol(ContentManagerViewControllerHolder)]) {
id<ContentManagerViewControllerHolder> holder = (id<ContentManagerViewControllerHolder>)fromViewController;
[[holder retreiveContentManagerController] push:viewController];
}
}
- (void)animateDismissalOfViewController:(NSViewController *)viewController fromViewController:(NSViewController *)fromViewController {
NSViewController *parent = [viewController parentViewController];
if ([parent class] == [ContentManagerViewController class]) {
ContentManagerViewController *manager = (ContentManagerViewController*)parent;
[manager pop];
}
}
#end
#implementation SegueBetweenEmbedded
- (void)perform {
SegueAnimator *animator = [[SegueAnimator alloc] init];
[self.sourceController presentViewController:self.destinationController
animator:(id<NSViewControllerPresentationAnimator>)animator];
}
#end
ContentManagerViewController.h
#import <Cocoa/Cocoa.h>
#interface ContentManagerViewController : NSViewController
-(void)push:(NSViewController*)viewController;
-(void)pop;
-(void)popToRoot;
#end
ContentManagerViewController.m
#import "ContentManagerViewController.h"
#import "BackForwardViewController.h"
#interface ContentManagerViewController ()
#property (weak) IBOutlet NSView *subViewControllerManager;
#property NSViewController *currentViewController;
#property NSMutableArray<NSViewController*> *viewControllerStack;
#end
#implementation ContentManagerViewController
-(instancetype)init {
self = [super init];
self.viewControllerStack = [NSMutableArray array];
return self;
}
-(instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
self.viewControllerStack = [NSMutableArray array];
return self;
}
-(instancetype)initWithNibName:(NSNibName)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
self.viewControllerStack = [NSMutableArray array];
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)showViewController:(NSViewController*)viewController {
[self addChildViewController:viewController];
viewController.view.frame = self.currentViewController.view.frame;
[self.view addSubview:viewController.view];
self.currentViewController = viewController;
}
-(void)removeCurrentViewControllerFromView {
[self.currentViewController.view removeFromSuperview];
[self.currentViewController removeFromParentViewController];
}
-(void)push:(NSViewController*)viewController {
[self removeCurrentViewControllerFromView];
[self.viewControllerStack addObject:viewController];
[self showViewController:viewController];
}
-(void)pop {
if (self.viewControllerStack.count > 1) {
[self removeCurrentViewControllerFromView];
[self.viewControllerStack removeLastObject];
NSViewController *viewController = [self.viewControllerStack lastObject];
[self showViewController:viewController];
}
}
-(void)popToRoot {
while (self.viewControllerStack.count > 1) {
[self pop];
}
}
-(void)prepareForSegue:(NSStoryboardSegue *)segue sender:(id)sender {
// this will be called on the initial embed to set up the first view controller
self.currentViewController = segue.destinationController;
[self.viewControllerStack addObject:segue.destinationController];
}
#end
BackForwardViewController.h
#import <Cocoa/Cocoa.h>
#interface BackForwardViewController : NSViewController
#end
BackForwardViewController.m
#import "BackForwardViewController.h"
#interface BackForwardViewController ()
#end
#implementation BackForwardViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
}
#end

iPhone SDK: Buttons and Labels causing UIScrollView to not scroll

I have been dealing with this all day and I simply cant find what the heck is going wrong. I have a scrollview inside a viewcontroller, but when i add a label or button to the view the scroll view stops scrolling. This is so standard i've done it hundred of times but for some reason its acting up now. Hope someone can help.
.h
#interface Test : UIViewController{
IBOutlet UIScrollView *scrollView;
}
-(IBAction)goBack;
#end
.m
#interface Test ()
#end
#implementation Test
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
scrollView.contentSize=CGSizeMake(320.0,1100.0);
}
-(IBAction)goBack
{
[self dismissModalViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Receiver is a forward class and corresponding #interface may not exist

thanks for taking the time to read my problem and help me:)
I am building a TabView project and on tab 3 of the app I have a row of albums, in album 3, page 1 the nextButton takes the user to page 2 of the album. Page 2 has more buttons, including a backPage(get back to page 1) and a backButton(to go back to the album selection page).
ERROR:
A3Page2ViewController.m:92:49: Property 'view' cannot be found in forward class object 'A3Page1ViewController'
WARNING:
A3Page2ViewController.m:91:28: Receiver 'A3Page1ViewController' is a forward class and corresponding #interface may not exist
I have searched this site and others and most of the answers are in relation to how #Class should be used in the header filw and #import ".h" should only be used in the .m file. I have checked my code and I was following the rules, so why does it still not work?
THIS IS WHAT I HAVE DONE: This is the function(in A3Page2ViewController) that is apparently making the error...
-(IBAction)backPage:(id)sender
{
a3Page1ViewController = [[A3Page1ViewController alloc]initWithNibName:#"A3Page1ViewController"bundle:nil];
[self.view addSubview:a3Page1ViewController.view];
}
Please note that when commented out, the app runs FINE all the views are loaded, all buttons including backButton work. When uncommented it just refuses to build, with the errors.
Below I list both A3Page1ViewController and A3Page2ViewController both the .h and .m files.
You can see that I have the #class and #import ".h" in the correct places.
WEIRDNESS!!
I actually have the exact same code WORKING in the 2nd tab, I use it to switch between views with out any problem. SO WHY DOES IT NOT WORK HERE?!?!
PLEASE HELP I AM STUCK ATM AND I DON'T KNOW WHY:/
A3Page1ViewController.h
#import <UIKit/UIKit.h>
#class A3Page2ViewController;
#class ThirdViewController;
#class A3P1;
#class A3P2;
#class A3P3;
#interface A3Page1ViewController : UIViewController {
A3Page2ViewController*a3Page2ViewController;
ThirdViewController*thirdViewController;
A3P1*A3P1;
A3P2*A3P2;
A3P3*A3P3;
UIButton *button1;
UIButton *button2;
UIButton *button3;
UIButton *nextButton;
UIButton *backButton;
}
#property(nonatomic,retain) IBOutlet A3Page2ViewController *a3Page2ViewController;
#property(nonatomic,retain) IBOutlet ThirdViewController *thirdViewController;
#property(nonatomic,retain) IBOutlet A3P1 *a3P1;
#property(nonatomic,retain) IBOutlet A3P2 *a3P2;
#property(nonatomic,retain) IBOutlet A3P3 *a3P3;
#property (nonatomic, retain) IBOutlet UILabel *logoLabel;
#property (nonatomic, retain) IBOutlet UILabel *descriptionLabel;
#property (nonatomic, retain) IBOutlet UILabel *copyrightLabel;
#property(nonatomic,retain) IBOutlet UIButton *button1;
#property(nonatomic,retain) IBOutlet UIButton *button2;
#property(nonatomic,retain) IBOutlet UIButton *button3;
#property(nonatomic,retain) IBOutlet UIButton *nextButton;
#property(nonatomic,retain) IBOutlet UIButton *backButton;
-(IBAction)FirstButton:(id)sender;
-(IBAction)SecondButton:(id)sender;
-(IBAction)ThirdButton:(id)sender;
-(IBAction)nextPage:(id)sender;
-(IBAction)backButton:(id)sender;
#end
A3Page1ViewController.m
#import "A3Page1ViewController.h"
#import "FXLabel.h"
#import <QuartzCore/QuartzCore.h>
#import "A3Page2ViewController.h"
#import "ThirdViewController.h"
#import "A3P1.h"
#import "A3P2.h"
#import "A3P3.h"
#implementation A3Page1ViewController
#synthesize a3Page2ViewController,thirdViewController,a3P1 ,a3P2 ,a3P3 , logoLabel, descriptionLabel, button1,button2,button3,nextButton,backButton, copyrightLabel;
-(UILabel*)createLabelWithFrame:(CGRect)frame andFontSize:(float)fontSize andText:(NSString*)text
{
UILabel* label = [[UILabel alloc] initWithFrame:frame];
[label setFont:[UIFont systemFontOfSize:fontSize]];
[label setTextColor:[UIColor whiteColor]];
[label setShadowColor:[UIColor blackColor]];
[label setShadowOffset:CGSizeMake(0, -1)];
//[label setTextAlignment:UITextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
[label setText:text];
return label;
}
-(IBAction)FirstButton:(id)sender
{
a3P1 = [[A3P1 alloc]initWithNibName:#"A3P1"bundle:nil];
[self.view addSubview:a3P1.view];
}
-(IBAction)SecondButton:(id)sender
{
a3P2 = [[A3P2 alloc]initWithNibName:#"A3P2"bundle:nil];
[self.view addSubview:a3P2.view];
}
-(IBAction)ThirdButton:(id)sender
{
a3P3 = [[A3P3 alloc]initWithNibName:#"A3P3"bundle:nil];
[self.view addSubview:a3P3.view];
}
-(IBAction)backButton:(id)sender
{
thirdViewController = [[ThirdViewController alloc]initWithNibName:#"ThirdView"bundle:nil];
[self.view addSubview:thirdViewController.view];
}
-(IBAction)nextPage:(id)sender
{
a3Page2ViewController = [[A3Page2ViewController alloc]initWithNibName:#"A3Page2ViewController"bundle:nil];
[self.view addSubview:a3Page2ViewController.view];
}
- (void)viewDidLoad
{
//CGRectMake(x,y,width,height);
// Do any additional setup after loading the view, typically from a nib.
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
A3Page2ViewController.h
#class A3Page1ViewController;
#class ThirdViewController;
#class A3P10;
#class A3P11;
#class A3P12;
#interface A3Page2ViewController : UIViewController {
A3Page1ViewController*a3Page1ViewController;
ThirdViewController*thirdViewController;
A3P10*a3P10;
A3P11*a3P11;
A3P12*a3P12;
UIButton *button1;
UIButton *button2;
UIButton *button3;
UIButton *backButton;
UIButton *backPage;
}
#property(nonatomic,retain) IBOutlet A3Page1ViewController *a3Page1ViewController;
#property(nonatomic,retain) IBOutlet ThirdViewController *thirdViewController;
#property(nonatomic,retain) IBOutlet A3P10 *a3P10;
#property(nonatomic,retain) IBOutlet A3P11 *a3P11;
#property(nonatomic,retain) IBOutlet A3P12 *a3P12;
#property(nonatomic,retain) IBOutlet UIButton *button1;
#property(nonatomic,retain) IBOutlet UIButton *button2;
#property(nonatomic,retain) IBOutlet UIButton *button3;
#property(nonatomic,retain) IBOutlet UIButton *backButton;
#property(nonatomic,retain) IBOutlet UIButton *backPage;
-(IBAction)FirstButton:(id)sender;
-(IBAction)SecondButton:(id)sender;
-(IBAction)ThirdButton:(id)sender;
-(IBAction)backButton:(id)sender;
-(IBAction)backPage:(id)sender;
#end
A3Page2ViewController.m
#import "A3Page1ViewController.h"
#import "FXLabel.h"
#import <QuartzCore/QuartzCore.h>
#import "A3Page2ViewController.h"
#import "ThirdViewController.h"
#import "A3P10.h"
#import "A3P11.h"
#import "A3P12.h"
#implementation A3Page2ViewController
#synthesize thirdViewController,a3Page1ViewController,a3P10 ,a3P11 ,a3P12, backButton,backPage, button1,button2,button3;
-(UILabel*)createLabelWithFrame:(CGRect)frame andFontSize:(float)fontSize andText:(NSString*)text
{
UILabel* label = [[UILabel alloc] initWithFrame:frame];
[label setFont:[UIFont systemFontOfSize:fontSize]];
[label setTextColor:[UIColor whiteColor]];
[label setShadowColor:[UIColor blackColor]];
[label setShadowOffset:CGSizeMake(0, -1)];
// [label setTextAlignment:UITextAlignmentCenter];
[label setBackgroundColor:[UIColor clearColor]];
[label setText:text];
return label;
}
-(IBAction)FirstButton:(id)sender
{
a3P10 = [[A3P10 alloc]initWithNibName:#"A3P10"bundle:nil];
[self.view addSubview:a3P10.view];
}
-(IBAction)SecondButton:(id)sender
{
a3P11 = [[A3P11 alloc]initWithNibName:#"A3P11"bundle:nil];
[self.view addSubview:a3P11.view];
}
-(IBAction)ThirdButton:(id)sender
{
a3P12 = [[A3P12 alloc]initWithNibName:#"A3P12"bundle:nil];
[self.view addSubview:a3P12.view];
}
-(IBAction)backButton:(id)sender
{
thirdViewController = [[ThirdViewController alloc]initWithNibName:#"ThirdView"bundle:nil];
[self.view addSubview:thirdViewController.view];
}
-(IBAction)backPage:(id)sender
{
//*****Alleged Error/Warning causing code=[********
a3Page1ViewController = [[A3Page1ViewController alloc]initWithNibName:#"A3Page1ViewController"bundle:nil];
[self.view addSubview:a3Page1ViewController.view];
}
- (void)viewDidLoad
{
//CGRectMake(x,y,width,height);
// Do any additional setup after loading the view, typically from a nib.
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end

Need help debugging switchChanged method

Am getting error ("switchChanged" undeclared) in the implementation file, but can't find the problem. Can you help me?
TIA
ViewController.m
#import "Control_FunViewController.h"
#implementation Control_FunViewController
#synthesize nameField;
#synthesize numberField;
#synthesize sliderLabel;
#synthesize leftSwitch;
#synthesize rightSwitch;
#synthesize doSomethingButton;
-(IBAction)sliderChanged:(id)sender
{
UISlider *slider = (UISlider *)sender;
int progressAsInt = (int)(slider.value + 0.5f);
NSString *newText = [[NSString alloc] initWithFormat:#"%d",progressAsInt];
sliderLabel.text = newText;
[newText release];
}
-(IBAction)textFieldDoneEditing:(id)sender
{
[sender resignFirstResponder];
}
-(IBAction)backgroundTap:(id)sender
{
[nameField resignFirstResponder];
[numberField resignFirstResponder];
}
-(IBAction)toggleControls:(id)sender
{
if ([sender selectedSegmentIndex] == kSwitchesSegmentIndex)
{
leftSwitch.hidden = NO;
rightSwitch.hidden = NO;
doSomethingButton.hidden = YES;
}
else {
leftSwitch.hidden =YES;
rightSwitch.hidden =YES;
doSomethingButton.hidden = NO;
}
-(IBAction)switchChanged:(id)sender
{
UISwitch *whichSwitch = (UISwitch *)sender;
BOOL setting = whichSwitch.isOn;
[leftSwitch setOn:setting animated:YES];
[rightSwitch setOn:setting animated:YES];
}
-(IBAction)buttonPressed
{
//TODO: Implement Action Sheet and Alert
}
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[nameField release];
[numberField release];
[sliderLabel release];
[leftSwitch release];
[rightSwitch release];
[doSomethingButton release];
[super dealloc];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#define kSwitchesSegmentIndex 0
#interface Control_FunViewController : UIViewController {
UITextField *nameField;
UITextField *numberField;
UILabel *sliderLabel;
UISwitch *leftSwitch;
UISwitch *rightSwitch;
UIButton *doSomethingButton;
}
#property(nonatomic,retain)IBOutlet UITextField *nameField;
#property(nonatomic,retain)IBOutlet UITextField *numberField;
#property(nonatomic,retain)IBOutlet UILabel *sliderLabel;
#property(nonatomic,retain)IBOutlet UISwitch *leftSwitch;
#property(nonatomic,retain)IBOutlet UISwitch *rightSwitch;
#property(nonatomic,retain)IBOutlet UIButton *doSomethingButton;
-(IBAction)textFieldDoneEditing:(id)sender;
-(IBAction)backgroundTap:(id)sender;
-(IBAction)sliderChanged:(id)sender;
-(IBAction)toggleControls:(id)sender;
-(IBAction)switchChanged:(id)sender;
-(IBAction)buttonPressed;
#end
The colon (:) is part of the name of the method, but you haven't included it in the error message. It may be that you just forgot, but if you're calling -switchChanged (no colon) from somewhere, or if you connected a control using the action -switchChanged (no colon), that's the problem. Perhaps you added the colon and sender parameter later on?

Resources