please help me to resolve this issue
i have a view controller in a navigation stack named firstviewcontroller
FirstViewController.h
#class ImperialPickerController;
#class FractionPickerController;
#class MetricPickerController;
#interface FirstViewController : UIViewController {
UIView *pickerViewContainer;
ImperialPickerController *ImperialPickerController;
FractionPickerController *FractionPickerController;
MetricPickerController *MetricPickerController;
UIView *ImperialPickerViewContainer;
UIView *FractionPickerViewContainer;
UIView *MetricPickerViewContainer;
UISegmentedControl *segmentedControl;
NSInteger selectedUnit;
}
#property(nonatomic,retain) IBOutlet UIView *pickerViewContainer;
#property(nonatomic,retain) IBOutlet UIView *ImperialPickerViewContainer;
#property(nonatomic,retain) IBOutlet UIView *FractionPickerViewContainer;
#property(nonatomic,retain) IBOutlet UIView *MetricPickerViewContainer;
#property(nonatomic,retain) IBOutlet ImperialPickerController *ImperialPickerController;
#property(nonatomic,retain) IBOutlet FractionPickerController *FractionPickerController;
#property(nonatomic,retain) IBOutlet MetricPickerController *MetricPickerController;
#property(nonatomic,retain) IBOutlet UISegmentedControl *segmentedControl;
-(IBAction)toggleUnit;
#end
FirstViewController.m
#implementation FirstViewController
#synthesize ImperialPickerController;
#synthesize FractionPickerController;
#synthesize MetricPickerController;
#synthesize ImperialPickerViewContainer;
#synthesize FractionPickerViewContainer;
#synthesize MetricPickerViewContainer;
#synthesize pickerViewContainer;
#synthesize segmentedControl;
define METRIC_INDEX 0
define IMPERIAL_INDEX 1
define FRACTION_INDEX 2
-(IBAction)toggleUnit
{
selectedUnit = [segmentedControl selectedSegmentIndex];
if (selectedUnit == METRIC_INDEX)
{
[MetricPickerController updateLabel1];
}
}
#end
MetricPickerController.h
#interface MetricPickerController : NSObject <UIPickerViewDataSource,UIPickerViewDelegate> {
UIPickerView *pickerView;
UILabel *label;
}
#property(nonatomic,retain)UIPickerView *pickerView;
#property(nonatomic,retain)UILabel *label;
-(void)updateLabel1;
#end
MetricPickerController.m
import "MetricPickerController.h"
#implementation MetricPickerController
#synthesize pickerView;
#synthesize label;
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 10;
}
-(void)updateLabel1
{
label.text = #"test"
}
the problem is that i get an error message on compiling here in the firstviewcontroller
-(IBAction)toggleUnit
{
selectedUnit = [segmentedControl selectedSegmentIndex];
if (selectedUnit == METRIC_INDEX)
{
[MetricPickerController updateLabel1]; <<<<< (MetricPickerController might not respond to +updateLabel1)!!
also if i click the toggle in IB xcode will crash with sigbart error
}
can anyone please help and advise what i have done wrong i think i have everything hooked up properly so i guess this is to do with my method declaration somehow
i know the code is incomplete at this stage but its driving me crazy trying to get rid of this error and i hope you can appreciate that i am just a learner
}
The problem is that updateLabel1 is an instance method, and you are sending it to a class (the +updateLabel1 instead of -updateLabel1 in the error message tells you this).
Since you've named your instance variables the same as the classes, you should be able to solve this by writing
[self.MetricPickerController updateLabel1];
instead - this makes it clear to the compiler you are referring to the instance variable and not the actual class.
Related
Here is the situation. I have a view controller titled "MyViewController." Within this view controller I have a text editing feature that uses subclassed buttons. The name of the UIButton Subclass is "ColorSwatch"
I have setup delegate/protocol methods in the "ColorSwatch.h" subclass as follow.
// ColorSwatch.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#protocol ColorSwatchDelegate <NSObject>
- (void)fontColor:(UIColor *)color;
#end
#interface ColorSwatch : UIButton {
id <ColorSwatchDelegate> colorSwatchDelegate;
CAGradientLayer *gradient;
UIView *currentView;
UIColor *fontColor;
}
#property (nonatomic, retain) id <ColorSwatchDelegate> colorSwatchDelegate;
#property (nonatomic, retain) CAGradientLayer *gradient;
#property (nonatomic, retain) UIView *currentView;
#property (nonatomic, retain) UIColor *fontColor;
#end
Now in my "ColorSwatch.m" I have:
// ColorSwatch.m
#import "ColorSwatch.h"
#import <QuartzCore/QuartzCore.h>
#import "MyViewController.h"
#implementation ColorSwatch
#synthesize gradient;
#synthesize currentView;
#synthesize colorSwatchDelegate;
#synthesize fontColor;
-(void)setupView{
"Makes the subclassed buttons pretty"
}
-(id)initWithFrame:(CGRect)frame{
if((self = [super initWithFrame:frame])){
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
if((self = [super initWithCoder:aDecoder])){
[self setupView];
MyViewController *mvc = [[MyViewController alloc] initWithNibName:
#"MyViewController" bundle:nil];
self.colorSwatchDelegate = mvc;
}
return self;
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self magnify:view];
fontColor = view.backgroundColor;
[self.colorSwatchDelegate fontColor:fontColor];
}
- (void)magnify:(UIView *)view
{
}
- (void)dealloc
{
[currentView release];
[gradient release];
[fontColor release];
[super dealloc];
}
#end
In the "MyViewController.h" I have:
// MyViewController.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "ColorSwatch.h"
#interface MyViewController : UIViewController <ColorSwatchDelegate> {
UITextField *photoLabelTextField;
}
#property (nonatomic, retain) IBOutlet UITextField *photoLabelTextField;
#end
In the "MyViewController.m" I have:
- (void)fontColor:(UIColor *)color
{
NSLog(#"Selected Font Color");
[self.photoLabelTextField setTextColor:color];
}
Now the delegate method sort of works, meaning when I tap on a color button the
NSLog(#"Selected Font Color");
message gets fired. But the problem is that I cannot change the
[self.photoLabelTextField setTextColor:color];
property. I have tried numerous ways of changing the property, the only thing that I can do is send NSLogs, anything I try to change a property in the "MyViewController" Class nothing happens.
If anyone could please help me out, I would appreciate it.
Thank you
The problem is that the ColorSwatch is sending delegate messages to a dangling instance of MyViewController that it incorrectly allocated in it's initWithCoder: method.
UIControls shouldn't allocate ViewControllers to be their delegates... it goes the other way around.
Delete these lines...
// in ColorSwatch.m initWithCoder:
MyViewController *mvc = [[MyViewController alloc] initWithNibName:
#"MyViewController" bundle:nil];
self.colorSwatchDelegate = mvc;
Then, in MyViewController.m ...
- (void)viewDidLoad {
ColorSwatch *colorSwatchButton = [[ColorSwatch alloc] buttonWithType:UIButtonTypeCustom];
// or place a ColorSwatch in the xib, on MyViewController's view... But not before you
// you delete lines from initWithCoder, otherwise it's infinite circular allocation
colorSwatchButton.frame = CGRectMake(/* ... */);
colorSwatchButton addTarget:self action:#selector(csButtonPressed:) forControlEvent: UIControlEventTouchUpInside];
// and so on...
// now the important part:
colorSwatchButton.colorSwatchDelegate = self;
// see - the ViewController is in charge of allocation, sets itself up as the delegate
[self.view addSubview:colorSwatchButton];
}
Instead of building the button in code, you can use IB.
Step 1: make the delegate an outlet...
#property (nonatomic, retain) IBOutlet id <ColorSwatchDelegate> colorSwatchDelegate;
Step 2: draw the buttons in IB, and set their class to ColorSwatch.
Then you can skip the code I wrote in viewDidLoad.
Step 3: The newly placed button should now present an outlet in IB. You can drag from that to the MyViewController as you normally do.
There might be a connection problem in your IBOutlet photoLabelTextField, you may have forgotten to connect xib text field with your photoLabelTextField
I am having some issues where my UITableView is not reloading, I have redone the linking of the outlet to make sure that was not the issue. I have also tried using [self table] reloadData] but that does not seem to work either. I have debugged the issues, but it simply skips reloadData and the app keeps running like the code is not even there.
Top part of my .m file, nothing is done in ViewDidLoad
#import "SettingsCourses.h"
#implementation SettingsCourses
#synthesize settingsCourses;
#synthesize Delegate;
#synthesize BackButton;
#synthesize DeleteButton;
#synthesize table;
#synthesize courses;
#synthesize dataHandler;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
dataHandler = [[DataHandler alloc] init];
dataHandler.coursesDelegate = self;
courses = [dataHandler fetchCourses];
NSLog(#"Debug Count: %i", [courses count]);
}
[table reloadData];
return self;
}
- (void) viewWillAppear:(BOOL)animated {
[table reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (courses) {
NSLog(#"Count: %i", [courses count]);
return [courses count];
}
else {
NSLog(#"Count: 0");
return 0;
}
}
My .h file
#import <UIKit/UIKit.h>
#import "dataHandler.h"
#interface SettingsCourses : UIViewController
#property (strong, nonatomic) DataHandler *dataHandler;
#property (strong, nonatomic) NSMutableArray *courses;
#property (strong, nonatomic) IBOutlet UIView *settingsCourses;
#property (nonatomic, strong) id Delegate;
#property (weak, nonatomic) IBOutlet UIButton *BackButton;
#property (weak, nonatomic) IBOutlet UIButton *DeleteButton;
#property (weak, nonatomic) IBOutlet UITableView *table;
- (IBAction)Delete:(id)sender;
- (IBAction)Back:(id)sender;
Thanks for any help!
add this lines:
- (void)viewDidLoad {
[super viewDidLoad];
self.table.dataSource = self;
self.table.delegate = self;
}
but much easier would be to set the datasource in interface-build aka the XIB file.
Try this
#interface SettingsCourses : UIViewController <UITableViewDelegate,UITableViewDataSource>
You do not appear to be setting the table delegate or datasource. To do this just add the following code when in your unit method
table.dataSource = self or wherever your data source is;
table.delegate = self:
YOu might want to try to disconnect and reconnect datasource and delegate in the interface builder. IB sometimes "forgets" connections.
I had setup a UITableViewController in IB, but its class name didn't match the UITableViewController file I had. Rookie!
The error for me was that I was returning 0 in
func numberOfSections(in tableView: UITableView) -> Int
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;
}
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
switching views in iphone
I am making a client server program in which i want to switch from one view to another but i am getting an error in "clientserverprogram view.m" plz help.i also asked same question five hours ago but the answer was not satisfactory again same error is pertaining.
"clientserverprogram view.h"
#import <UIKit/UIKit.h>
#class secondview;
#interface clientserverprogramViewController : UIViewController {
IBOutlet UITextField *name;
IBOutlet UITextView *filepath;
IBOutlet UIButton *print;
IBOutlet UIButton *settings;
IBOutlet UIButton *cancel;
IBOutlet UILabel *display;
IBOutlet secondview *secondview;
}
-(IBAction) print;
-(IBAction) settings;
-(IBAction) cancel;
#property (nonatomic , retain) IBOutlet UITextField *name;
#property (nonatomic , retain) IBOutlet UITextView *filepath;
#property (nonatomic , retain) IBOutlet UILabel *display;
#end
"clientserverprogram view.m"
#import "clientserverprogramViewController.h"
#import "secondview.h"
#implementation clientserverprogramViewController
#synthesize name ,filepath,display ;
-(IBAction) print {
NSString *str = name.text;
[display setText : str];
}
-(IBAction) settings {
[self presentModalViewController: secondview animated: YES ];
"" error: expected expression before 'secondview'""
}
-(IBAction) cancel {
exit(0);
}
- (void)dealloc {
[super dealloc];
}
#end
"secondview.h"
#import <UIKit/UIKit.h>
#interface secondview : UIViewController {
IBOutlet UIView *view;
IBOutlet UIButton *back;
}
-(IBAction) back;
#end
""secondview.m""
#import "secondview.h"
#implementation secondview
-(IBAction) back {
[self.parentViewController dismissModalViewControllerAnimated: YES];
}
- (void)dealloc {
[super dealloc];
}
#end
I used your code and the problem here is your class name "secondview" and the instance you are making from it IBOutlet secondview *secondview are same. Please use different name for class names and the instances you create.
Always start with uppercase for class names and start with lowercase for class instances you create.
Hence, your class name should be SecondView and you should write IBOutlet SecondView *secondView.
Or you should just use different names. Its very confusing.
I have a simple project to present a modal view controller and transfer back a string based on which button in the modal VC that gets pressed. I based it all on watching the Stanford class on iTunes U. It looks like I have everything correct, but I get a couple of compiler warnings.
First I get one called passing argument 1 of 'setDelegate:' from incompatible pointer type in TransferViewController.m
Second I get four warnings called Invalid receiver type 'id <MyModalViewControllerDelegate>*' but these aren't displayed in the build results area, rather next to the offending lines in MyModalViewController.m, both lines in each of the button actions.
Here's the code...
// TransferViewController.h
#import <UIKit/UIKit.h>
#import "MyModalViewController.h";
#interface TransferViewController : UIViewController <MyModalViewControllerDelegate> {
UILabel *label;
UIButton *button;
}
#property (nonatomic, retain) IBOutlet UILabel *label;
#property (nonatomic, retain) UIButton *button;
- (IBAction)updateText;
#end
// TransferViewController.m
#import "TransferViewController.h"
#implementation TransferViewController
#synthesize label;
#synthesize button;
- (IBAction)updateText {
MyModalViewController *myModalViewController = [[MyModalViewController alloc] init];
myModalViewController.delegate = self; // I get the warning here.
[self presentModalViewController:myModalViewController animated:YES];
[myModalViewController release];
}
- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog {
label.text = selectedDog;
[self dismissModalViewControllerAnimated:YES];
}
#end
// MyModalViewController.h
#import <UIKit/UIKit.h>
#protocol MyModalViewControllerDelegate;
#interface MyModalViewController : UIViewController {
UIButton *abby;
UIButton *zion;
id <MyModalViewControllerDelegate> delegate;
}
#property (assign) id <MyModalViewControllerDelegate> delegate;
- (IBAction)selectedAbby;
- (IBAction)selectedZion;
#end
#protocol MyModalViewControllerDelegate <NSObject>
#optional
- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog;
#end
// MyModalViewController.m
#import "MyModalViewController.h"
#implementation MyModalViewController
#synthesize delegate;
- (IBAction)selectedAbby {
if ([self.delegate respondsToSelector:#selector (myModalViewController:didFinishSelecting:)]) {
[self.delegate myModalViewController:self didFinishSelecting:#"Abby"];
}
}
- (IBAction)selectedZion {
if ([self.delegate respondsToSelector:#selector (myModalViewController:didFinishSelecting:)]) {
[self.delegate myModalViewController:self didFinishSelecting:#"Zion"];
}
}
Get rid of those *s after id <something> and before delegate.
So make this
id <MyModalViewControllerDelegate> *delegate;
this
id <MyModalViewControllerDelegate> delegate;