Using UISegmentedControl in Xcode as RadioButtons - iphone

I have just recently started teaching myself Objective-C and iphone/ipad development. With my only prior experience in coding involving Java/Android, I have been having some problems with this new language.
I am trying to use UISegmentedControl in this app to decide which view will be shown when the "members" button is pressed. I do this by grabbing the current selected text from the UISegmentedControl, named "memberPicker", when the "members" button is pressed and then check if it is either #"Current" or #"Alumni" to decide what view is shown.
This function appears to be working correctly, however, when I click the "Alumni" button in the UISegmentedControl, the app crashes with the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainPageViewController MemberSegmentedControl:]: unrecognized selector sent to instance 0x7557250'
I believe my problem is with declaring or setting my UISegmentedControl properly, I understand it can take an array and ect. but I only need it to alternate between selecting Current or Alumni, and then my "members" button will do the rest. Any ideas on how to resolve this issue?
Here is the code from my .m view
#import "MainPageViewController.h"
#import "AKPsiAppViewController.h"
#interface MainPageViewController ()
#property (weak, nonatomic) UISegmentedControl *memberPicker;
#end
#implementation MainPageViewController
#synthesize memberPicker = _memberPicker;
- (IBAction)membersButtonPressed:(UIButton *)sender
{
if([[self.memberPicker titleForSegmentAtIndex: self.memberPicker.selectedSegmentIndex] isEqualToString:#"Current"])
{
[self performSegueWithIdentifier:#"Current Member Segue" sender:self];
}
}
#end

You probably have connected the UISegmentedControl in Interface Builder to a IBOutlet named MemberSegmentedControl that you later removed in the #interface of your class. Check the connections to the UISegmentedControl in Interface Builder if there is not an additional connection besides the memberPicker.
Hope it helps!

The error you are getting means that you are calling the IBAction/function MemberSegmentedControl: which does not exist in the .m file. Maybe you had previously created this function and connected it with Value Changed of UISegmentedControl but you did not write its implemention (in .m file). Maybe you have deleted this but the connection still exists. To check this, please follow the following steps.
Open Interface Builder.
Click on the UISegmentedControl.
Open Connections Inspector (Click on View Menu, Click on Utilities and then Click on "Show Connections Inspector". Alternatively you can press Command+Options+6)
Under Sent Events, check if there is any of the events (especially Value Changed) connected to "File owner's MemberSegmentedControl:". If yes, then remove it by clicking on the x button and then try again.
If your problem doesn't get resolved, please do the following
Share a screenshot of the Interface Builder file (XIB file)
Share the header file (.h file)

Related

Xcode: Button clicked results in NSInvalidArgumentException

I'm working in Xcode 4.3.2.
I am new to Xcode. I'm building an application that must change to different views on button clicks. My files are: AppDelegate.h/.m, GreenViewController.h/.m, SwitchViewController.h/.m, GreenView.xib - I am not using storyboards, but my project demands that I don't use them (backwards compatibility issues).
Here's my problem (it seems very simple): I'm trying to print to the console when a UIButton (placed in GreenView.xib) is clicked. Here's my code for GreenViewController.h
#import <UIKit/UIKit.h>
#interface GreenViewController : UIViewController
- (IBAction)switchViews:(id)sender;
#end
Here's my (deprecated) code for GreenViewController.m:
#import "GreenViewController.h"
#implementation GreenViewController
- (IBAction) switchViews:(id)sender {
NSLog(#"Button Pressed!");
}
The owner for GreenView.xib is GreenViewController.
For some reason I have the error only when the UIButton (in GreenView.xib) is pressed:
2012-10-09 18:07:38.490 MyViewSwitcher[8655:f803] -[SwitchViewController switchViews:]: unrecognized selector sent to instance 0x688a660
2012-10-09 18:07:38.492 MyViewSwitcher[8655:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SwitchViewController switchViews:]: unrecognized selector sent to instance 0x688a660'
It seems that SwitchViewController is expecting something from the method "switchViews", but "switchViews" is only listed in GreenViewController. Before, I had "switchViews" in SwitchViewController, but I deleted all the code corresponding to the method & all the connections. Again, I've double checked that "switchViews" in GreenViewController is connected to the UIButton found in GreenView.xib. I've already cleaned & re-built my project & I still get this error.
Thanks for your help!
What your error is saying is that you are calling the switchViews: method on an instance of SwitchViewController. And since there is no definition for switchViews: (because you deleted it) for the class SwitchViewController, it doesn't know what to do, and crashes.
Hate to tell ya this, but your button is connected to the switchViews: method of a SwitchViewController. You say "I've double checked that 'switchViews' in GreenViewController is connected to the UIButton found in GreenView.xib". Well, yea, from your crash, it is. But are you sure its connected to the switchViews: function of a GreenViewController instance? How did you check this information?
What I would suggest is deleting all connections to the UIButton in the connection inspector. Then reconnecting it to the view controller (which you say is a GreenViewController). It should then bring up the list of IBActions which should only be the switchViews: method.
If you do that, and it still doesn't work. Try deleting the button and remaking it, then reconnecting it.

File's Owner didn't received Action

the ping1 and how1 below method didn't called from UIButton
so i was double checking it in xib what i am wrong.
but i can't found the problem.
to solve this only i can do is looking the xib and code
is there any know-how available to solve it?
something like insert some code when UIButton send event
anyone advice me it will be thankful
bonhyoung.
#interface ViewController : UIViewController
-(IBAction)ping1:(id)sender;
-(IBAction)how1:(id)sender;
#end
#implementation ViewController
-(IBAction)ping1:(id)sender
{
// i put the break point in here
}
-(IBAction)how1:(id)sender
{
// i put the break point in here
}
// some other code...
#end
You should connect some IBOutlets and check in viewDidLoad that your connections are right (ie that your IBOutlets are not nil once the view is loaded).
First make sure that the file owner's class is set to ViewController. This is in the IB pane, click on the File's Owner and select the 3rd tab on the right panel. It should say ViewController and not UIViewController.
Second make sure your actions are setup. Right click on the button, and make sure the Touch Up Inside is wired to the correct method.

Connecting an IBOutlet to a view within a scrollview within a tab view crashes app. Any ideas?

I used the tabview setup to create an app that has several tabs. One tab has a UIScrollView on it which loads 12 different views into 12 pages. I tried adding a button and imageview to one of the views and now it crashes. I've pored over every single question on here I can find, and have tried every suggested solution, but to no avail. The error that I get is the famous "this class is not key value coding-compliant for the key foo" error. As with everyone else who had this problem, if I disconnect the outlet, it works fine. I've checked and rechecked the class for the nib and it points to the right place.
So I'm not even sure where to go next.
I could post some code, but I don't even know what code would be helpful to post. I'm hoping someone just says, "Um yeah, you can't have a scroll view with multiple views in it and expect to have functionality on each page." That would be helpful!
Page2.h:
#import <UIKit/UIKit.h>
#interface Page2 : UIViewController {
IBOutlet UIImageView *infoImage;
}
#property (retain, nonatomic) UIImageView *infoImage;
(IBAction) showInfo:(id)sender;
#end
Page2.m:
- (IBAction)showInfo:(id)sender {
// do something please!
}
Then in the nib file I CTRL-dragged from "touch-up inside" on my button to the Files Owner and chose "showInfo" ... and I CTRL-dragged from Files Owner down to my UIImageView and chose infoImage.
CRASH
I am not cool enough to insert an image: http://i.stack.imgur.com/KjWhk.png
You still have an outlet connected in your nib that you no longer have defined in your class. You'll see in the inspector that it has turned gray. Disconnect it, and the error should be gone.
are u able to create any other outlet in the same Nib file, as i used to get the same crash, and it got fixed only when i duplicated the same nib, i had implemented almost all methods to solve it. If i am not wrong , this link can help you out https://discussions.apple.com/thread/2431110?start=0&tstart=0
I have sent you the email and attached your project. Hope its working at your end now.
Happy Coding!

Controlls are not displaying in interface builder?

i am facing a problem with IBOutlet,please Respected developers help me out, as i am fresher in iphone
for eg.
i declared buttons or controls with IBOutlet in .h file ,but many times interface builder is not showing the controls so i use to exit the application and reopen it again and i found it
so is there any thing i am missing.
thanks in advance...
Did you save your files? A mistake a lot of people new to iOS/Mac development don't realize is that Interface Builder will not show the IBOutlets unless you have saved the files first. So anytime you add an IBOutlet, make sure you save your header file before diving into Interface Builder.
How are you declaring your outlets? Also, what do you mean by "interface builder is not showing the controls"? If you have the following code:
#interface MyViewController : UIViewController {
IBOutlet UIButton *myButton;
IBOutlet UISlider *mySlider;
}
// Properties
// Methods
#end
In IB's connection inspector the outlets should show up and allow you to connect them to valid UI objects. Could you please clarify your issue?

iPhone: Proper use of View and View Controller

I've recently been doing a lot of Objective-C programming, and just got back into doing more iPhone development. I've done a lot of programming using MVC in other languages/frameworks, but I just want to make sure I'm using MVC properly in my iPhone Development.
I created a new iPhone Utility Application, which creates two views: MainView and FlipsideView. Both have a controller (FlipsideViewController and MainViewController) and XIB file of their own.
What I've been doing is putting the IBOutlet UIControl myControl variables in my MainView.h or FlipsideView.h files and then tying the controls in Interface Builder to those variables. Then I put any IBAction SomeAction myAction methods in the MainViewController.h and FlipsideViewController.h files and tying the events to those methods in Interface Builder.
This seems to be conceptually correct, but seems to cause problems. Say I have a button that when clicked it changes a label's text. So the Controller doesn't have a clue of what the variable name of the label is in the OnTouchUp event handler for my button. So I make a #property for it. But since the MainViewController.view property isn't of type MyView, I get warnings or errors whenever I try to access those properties from the view controller.
I am doing this correctly? Is there a better way to do this? If this is correct, how do I properly work with those variables without getting warnings or errors?
Thanks
Here's some code showing what I'm doing:
MainView.h
#import ...
#interface MainView : UIView
{
IBOutlet UILabel* label;
IBOutlet UIButton* button;
}
#property UILabel* label;
#property UIButton* button;
#end
MainViewController.m
-(void) buttonTouchUp:(id) sender
{
self.view.label.text = #"The button was pressed!"; //This gives error because label is not in the view structure or union
[self.view label].text = #"The button was pressed!"; //This gives a warning
}
I know I can cast the view to be of the right type, but that seems like a hack job.
I know I can live with the warning, but a warning says to me that I'm doing something that I probably shouldn't be doing. I don't believe the SDK would set me up to do have to do something like that all the time.
I must just be misunderstanding what the View is supposed to be and I'm using it incorrectly.
Most code I've seen and written myself keeps all the outlets and the actions on the controller. The button sends a message to the controller (via IBAction), then the controller updates the label (via IBOutlet). In other words, putting outlets on your views is unusual.
Views shouldn't be connected to other views unless they have some special relationship, like maybe back/forward buttons that target a UIWebView... but even then, you find out you need a controller to enable/disable the buttons as appropriate.