I am working with an iPhone application in Xcode and I want to use a segmented control with 4 different choices.
I am wondering what I am doing wrong and why the NSLog messages don't show when I click on it?
This is my code:
.h file:
#import <Foundation/Foundation.h>
#interface Format : UIViewController{
IBOutlet UISegmentedControl *papper;
}
#property (nonatomic, retain) IBOutlet UISegmentedControl *papper;
-(IBAction)papperskontrollSwitched:(id)sender;
.m file:
#import "Format.h"
#implementation Format
#synthesize papper;
-(IBAction)papperskontrollSwitched:(id)sender{
if(papper.selectedSegmentIndex==0){
NSLog(#"A5 valdes");
}
else if(papper.selectedSegmentIndex==1){
NSLog(#"A4 valdes");
}
else if(papper.selectedSegmentIndex==2){
NSLog(#"A3 valdes");
}
else if(papper.selectedSegmentIndex==3){
NSLog(#"Visitkort valdes");
}
}
#end
Try deleting the stuff in the brackets:
{
IBOutlet UISegmentedControl *papper;
}
There's no need for it when you have this:
#property (nonatomic, retain) IBOutlet UISegmentedControl *papper;
Having an ivar and a #property shouldn't matter here and indeed when I'm using this exact code in a test project it is working perfectly.
Ensure your UISegmentedControl is connected to the papper outlet in Interface Builder and you've connected the UISegmentedControl's 'Value Changed' action to -(IBAction)papperskontrollSwitched:
Related
I am not sure what is causing this problem, but I now cannot create outlets. When I create an outlet it appears and works fine in code, but does not appear under the view controller in storyboard. Old outlets have a warning symbol on them like this
When I remove this outlet, its gone and does not appear back to connect. I am running Xcode beta 6.2 because moving to 6.2 temporarily fixed this problem because I was having it beforehand in 6.1.
Here is the .h file of this class
#import <UIKit/UIKit.h>
#interface DashboardViewController : UITableViewController {
NSString *currentDay;
}
#property (nonatomic, weak) IBOutlet UILabel *dayLabel;
#property (nonatomic, weak) IBOutlet UILabel *blockLabel;
#property (nonatomic, weak) IBOutlet UILabel *currentClassLabel;
#end
and here are the outlets listed in storyboard
These are the details of the warning, but this is .h file's code for this class
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface InfoViewController : UIViewController {
IBOutlet UIScrollView *AISScrollView;
IBOutlet MKMapView *AISMapView;
}
-(IBAction)studentHandbook:(id)sender;
#end
Oddly enough, I ended up finding a solution and that was moving the project out of the directory that it was in. It was in my Dropbox folder and moving it out fixed. If anyone has this problem in the future and also happens to have the proj in their Dropbox, move it out.
try to change this:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface InfoViewController : UIViewController {
IBOutlet UIScrollView *AISScrollView;
IBOutlet MKMapView *AISMapView;
}
-(IBAction)studentHandbook:(id)sender;
#end
to that:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface InfoViewController : UIViewController {
}
#property (nonatomic,weak) IBOutlet UIScrollView *AISScrollView;
#property (nonatomic,weak) IBOutlet MKMapView *AISMapView;
-(IBAction)studentHandbook:(id)sender;
#end
Just in case someone suffers from the same thing again, the best answer inside this other thread did the trick for me:
XCode 6: can't connect any IBOutlet to ViewController
Removing the .h and .m files from the project (their references) and readding them again was the solution that worked for me.
I am trying to create a generic view controller and 2 other views which both will inherit from.
Now this is the generic view (what i think is relevant - if something else is needed i will add it):
#interface CardGameViewController ()
#property (strong, nonatomic) IBOutlet UITabBarItem *TabSelection;
#property (strong, nonatomic) CardMatchingGame *game;
#property (strong, nonatomic) IBOutlet UIButton *resetButton;
#property (weak, nonatomic) IBOutlet UILabel *scoreLable;
#property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
#end
#implementation CardGameViewController
- (CardMatchingGame*)game
{
if(!_game) _game = [[CardMatchingGame alloc]initWithCardCount:[self.cardButtons count] usingDeck:[self createDeck] gameMode:[self getActiveTabWithString:self.TabSelection.description]];
return _game;
}
- (Deck*)createDeck //abstract
{
return nil; <------- Relevant generic function
}
and these are the 2 files which inherit the mentioned file:
SetGameViewController.h:
#import "CardGameViewController.h"
#interface SetGameViewController : CardGameViewController
#end
SetGameViewController.m:
#import "SetGameViewController.h"
#import "SetCardDeck.h"
#interface SetGameViewController ()
#end
#implementation SetGameViewController
-(Deck*)createDeck
{
return [[SetCardDeck alloc]init];
}
#end
and the second one:
PlayingCardGameViewController.h:
#import "CardGameViewController.h"
#interface PlayingCardGameViewController : CardGameViewController
#end
PlayingCardGameViewController.m:
#import "PlayingCardGameViewController.h"
#import "PlayingCardDeck.h"
#interface PlayingCardGameViewController ()
#end
#implementation PlayingCardGameViewController
- (Deck*)createDeck
{
return [[PlayingCardDeck alloc]init];
}
#end
I also have tab bar navigator to switch between the 2 views.
The thing is, the first (SetGameViewController) is never instantiated no matter what i do,
Meaning the createDeck function is never called,
While the second one (PlayingCardGameViewController) is ok every time.
What i have tried:
placing break points on the main view - (CardMatchingGame*)game function.
this one only gets called when i am trying to start the second working view, and not the bad one.
If there is anything missing to give a certain lead, please let me know and i will add it.
Set breakpoints on the empty createDeck method in the base class, as well as on the method in your two subclasses.
My guess is that when you set up your tab bar controller in IB (Interface Builder) , you made one of the tabs be an instance of the generic CardGameViewController instead of a SetGameViewController.
I have checked thoroughly for a solution to this (notably here: Can't connect IBOutlet in Interface Builder) but cannot see the solution. I have a UITextView that I am using as a text area in a form. I have connected it to this class member in IB:
IBOutlet UITextView *notes;
here is my .h
#import <UIKit/UIKit.h>
#import <AddressBookUI/AddressBookUI.h>
#interface U2MIDetailController : UIViewController <UITextFieldDelegate, UITextViewDelegate> {
IBOutlet UIButton *confirmButton;
IBOutlet UITextView *notes;
}
#property (nonatomic, retain) IBOutlet UITextView* notes;
#property ABRecordRef personObject;
#property (strong, nonatomic) IBOutlet UIScrollView *scroller;
#end
and my .m, the relevant bits:
#synthesize notes;
- (void)viewDidLoad
{
[super viewDidLoad];
//set up delegates for keyboard hiding
notes.delegate = self;
notes.text = #"Notes";
...
}
In the links I've found some have solved this issue by checking the File's Owner "Class" attribute on the Identity inspector. How do I do that? I have attached a pic of the hierarchy which looks correct to me, the identity inspector doesn't jump out at me either as having any suspicious properties.
Here is a shot of the storyboard:
and here is how it looks int he simulator:
check your scrollview frame either it is fit to frame or not. put blackground color you know where you did mistake
I im fairly new to ios development, and following the tutorial exercise on chapter 4 of the book 'beginning ios5 development' i have run into some compile errors on my code.
here's what my header code .h file looks like
#import <UIKit/UIKit.h>
#interface BIDViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *namefIELD;
#property (strong, nonatomic) IBOutlet UITextField *numberField;
#property (strong, nonatomic) IBOutlet UILabel *sliderLabel;
#property (strong, nonatomic) IBOutlet UISegmentedControl *leftSwitch;
#property (strong, nonatomic) IBOutlet UISegmentedControl *rightSwitch;
#property (strong, nonatomic) IBOutlet UIButton *doSomethingButton;
-(IBAction)textFieldDoneEditing:(id)sender;
//to initialize the 'done' button when editing to confirm you have finished editing/
-(IBAction) backgroundTap:(id)sender;
- (IBAction)sliderChanged:(id)sender;
- (IBAction)switchChanged:(id)sender;
- (IBAction)toggleControls:(id)sender;
- (IBAction)buttonPressed:(id)sender;
#end
and here's my .m code where im facing the error "no visible #interface for 'UISegmentedControl' declares the selector 'setOn:animated;'
#synthesize leftSwitch;
#synthesize rightSwitch;
- (IBAction)switchChanged:(id)sender {
UISwitch *whichSwitch = (UISwitch *)sender;
BOOL setting = whichSwitch.isOn;
[leftSwitch setOn: setting animated: YES]; //the error lies here/
[rightSwitch setO: setting animated: YES]; //the error lies here/
}
- (IBAction)toggleControls:(id)sender {
}
- (IBAction)buttonPressed:(id)sender {
}
#end
the error statement is "no visible #interface for 'UISegmentedControl' declares the selector 'setOn:animated;'
pls help, that would be highly appreciated and voted for :)
#PhilipMills ...here's what i'm talking about, as soon as i click on a leftside or rightside of the segmented control object a break point is automatically made on my delegated header code here
#import <UIKit/UIKit.h>
#import "BIDAppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([BIDAppDelegate class])); //the breakpoint appears here.
}
}
A UISwitch has a setOn:animated: method but a UISegmentedControl does not. I'll guess that you assigned objects to leftSwitch and rightSwitch that are not the kind you intended (or the tutorial intended).
I have created one segmented control and a text view in my .xib file and I have declare it in .h file as
#interface controlsViewController : UIViewController {
IBOutlet UISegmentedControl *colorChooser;
IBOutlet UITextView *setText;
}
#property (nonatomic,retain) UISegmentedControl *colorChooser;
#property (nonatomic,retain) UITextView *setText
but it shows me warning on both lines of #property
Can anyone tell me why it warns me?
My guess would be that you don't have a #synthesize or #dynamic in your implementation. That would generate compiler warnings.
The position of the IBOutlet wound not generate a compiler warning as it's a marco of nothing. It's used by xcode resource editor (or the older Interface Builder) to indicate it's a outlet property and doesn't generate any code.
#interface controlsViewController : UIViewController {
UISegmentedControl *colorChooser;
UITextView *setText;
}
#property (nonatomic,retain) IBOutlet UISegmentedControl *colorChooser;
#property (nonatomic,retain) IBOutlet UITextView *setText;
You need to synthesize the property in your .m file. You won't get the error then.