Novice iOS programmer here, so sorry if I'm missing something easy, but...
I have a UIViewController class called LVSTSPMasterViewController whose view was built in IB. The view contains a UIScrollView (added in IB) and that scroll view has a subview of type LVSTSPView. LVSTSPView has a controller of type LVSTSPViewController.
I want to respond to touches in the LVSTSPView, so I added gesture recognizers to LVSTSPViewController.m. When I execute the gesture (e.g., do a long press), the code crashes with message "EXC_BAD_ACCESS (code=1, address=...)" in main.m.
Relevant code:
In LVSAppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
// Get pointer to app bundle
NSBundle *appBundle = [NSBundle mainBundle];
// Get xib file
LVSTSPMasterViewController *tspmvc = [[LVSTSPMasterViewController alloc] initWithNibName:#"LVSTSPMasterViewController" bundle:appBundle];
self.window.rootViewController = tspmvc;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
In LVSMasterViewController.m:
#interface LVSTSPMasterViewController () <UIScrollViewDelegate>
// IBOutlet declarations
#property (nonatomic, weak) IBOutlet UIScrollView *TSPScrollView;
// Pointers for convenience
#property (strong, nonatomic) LVSTSPView *TSPView;
#end
#implementation LVSTSPMasterViewController
- (void)viewDidLoad
{
// Create LVSTSPViewController
LVSTSPViewController *tspvc = [[LVSTSPViewController alloc] init];
// Set up pointer to LVSTSPView
self.TSPView = (LVSTSPView *)tspvc.view;
// Set frame of LVSTSPView
self.TSPView.frame = self.TSPScrollView.bounds;
// Set tspvc's view as subview of TSPScrollView
[self.TSPScrollView addSubview:tspvc.view];
// Set up scroll view
self.TSPScrollView.pagingEnabled = NO;
self.TSPScrollView.contentSize = self.TSPView.frame.size;
self.TSPScrollView.minimumZoomScale = 1.0;
self.TSPScrollView.maximumZoomScale = 3.0;
// Set scroll view's delegate property
self.TSPScrollView.delegate = self;
}
In LVSTSPViewController.m:
- (void)loadView
{
// Create view
LVSTSPView *view = [[LVSTSPView alloc] initWithFrame:CGRectZero];
self.view = view;
// Long-press recognizer
UILongPressGestureRecognizer *pressRecognizer =
[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[self.view addGestureRecognizer:pressRecognizer];
}
- (void)longPress:(UIGestureRecognizer *)gr
{
NSLog(#"longPress:");
}
One other note: If I set up the gesture recognizer in viewDidLoad: in LVSTSPMasterViewController.m, like so --
UILongPressGestureRecognizer *pressRecognizer =
[[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[self.TSPView addGestureRecognizer:pressRecognizer];
-- (and of course add longPress: to LVSTSPMasterViewController.m), then it works. But this doesn't seem like the right approach since the LVSTSPMasterViewController is not the view controller for LVSTSPView.
Any help will be appreciated!
Your view controller LVSTSPViewController is not retained anywhere or in other words your master view controller needs to retain it.
In LVSMasterViewController.m, change your code to
#implementation LVSTSPMasterViewController
{
LVSTSPViewController *tspvc;//creates strong reference to self(LVSTSPMasterViewController)
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
tspvc = [[LVSTSPViewController alloc] init];
// Set up pointer to LVSTSPView
self.TSPView = (LVSTSPView *)tspvc.view;
// Set frame of LVSTSPView
self.TSPView.frame = self.TSPScrollView.bounds;
// Set tspvc's view as subview of TSPScrollView
[self.TSPScrollView addSubview:tspvc.view];
// Set up scroll view
self.TSPScrollView.pagingEnabled = NO;
self.TSPScrollView.contentSize = self.TSPView.frame.size;
self.TSPScrollView.minimumZoomScale = 1.0;
self.TSPScrollView.maximumZoomScale = 3.0;
// Set scroll view's delegate property
self.TSPScrollView.delegate = self;
}
Also always call [super viewDidLoad] first in your viewDidLoad method. I would suggest to read more about view and view controller in the apple documentation. You necessarily do not need a view controller with every view. It all depends on your specific case. One view controller can manage multiple views.
Related
I have 2 Custom View classes(CustomView_A, CustomView_B) derived from UIView. I have UIViewController that should be able to switch between views at run-time..
What so far, I have done is.. in the Storyboard, I am using CustomView_A class as the View class.
#interface MyViewController: UIViewController
#property (nonatomic, weak) CustomView_A *customView_A;
Now I have the second CustomView_B class and I want to change view of MyViewController's view to CustomView_B at run-time.
How can I do that? Thanks in advance..
okay, here is the code as you want -
in your MyViewController.h put -
#property (nonatomic, retain) CustomView_A *customView_A;
#property (nonatomic, retain) CustomView_B *customView_B;
-(void)switchView; // to switch the views.
in your MyViewController.m put -
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.customView_A = [[CustomView_A alloc]initWithFrame:self.view.frame];
self.customView_A.backgroundColor = [UIColor redColor];
UIButton *trigger = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // Just take this button so that your switchView methods will get called on click of this method.
[trigger setFrame:CGRectMake(10, 10, 50, 30)];
[trigger setTitle:#"Click" forState:UIControlStateNormal];
[trigger addTarget:self action:#selector(switchView) forControlEvents:UIControlEventTouchUpInside];
[self.customView_A addSubview:trigger];
[self.view addSubview:self.customView_A];
self.customView_B = [[CustomView_B alloc]initWithFrame:self.view.frame];
self.customView_B.backgroundColor = [UIColor yellowColor];
self.customView_B.hidden = YES;
[self.view addSubview:self.customView_B];
}
- (void)switchView
{
[UIView animateWithDuration:10 delay:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.customView_A.hidden = YES;
self.customView_B.hidden = NO;
} completion:nil];
}
Do opposite when you again want to switch the views.
Don't. What you're describing is an essential misunderstanding of UIViewController. Once a UIViewController instance has a view, that is its view forever.
If you want two different views then either:
Use two view controllers (for example, you can present view controller B and its view on top of view controller A and its view, using a modal segue), or
Make at least one of those views not be owned by a view controller: just place that view in front of the other view and later remove it again, at will.
Try this:
- (IBAction)changeView {
if (self.customView_A.hidden == YES) {
self.customView_A.hidden = NO;
self.customView_B.hidden = YES;
//You should use a UIView animation here to do this.
}
else {
self.customView_A.hidden = YES;
self.customView_B.hidden = NO;
//Same here
)
}
In your viewDidLoad add the view to CGRectZero
- (void)viewDidLoad {
self.customView_A = [[CustomView_A alloc]initWithFrame:CGRectZero];
[self.view addSubview:self.customView_A];
//do the same with the other custom view
}
Sorry if the code is a little faulty, I didn't use Xcode to type this up.
Where should I initialize a UITablewView as a subview, in - (id)initWithFrame:(CGRect)frame, in viewdidload or loadView? Which is the better approach? Where should I make the frame (I mean which is more effective)?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = kViewBackgroundColor;
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_tableView.backgroundView = nil;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.bounces = NO;
[self addSubview:_tableView];
}
return self;
}
On my projects I usually create a baseViewController with a custom initialiser like this:
- (id)init{
self = [self initWithNibName:NSStringFromClass([self class]) bundle:[NSBundle mainBundle]];
if (self) {
//You should create the tableView and other properties here
//and add as subviews inside viewDidLoad
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,100,100)];
}
return self;
}
and I always create my view controller by using this init method, because I don't think the other vc's need to know the name of the nib file. If I were you I would create subviews inside init method, add as subviews inside viewDidLoad or viewWillAppear and finally releasing them inside dealloc if you are not using ARC.
Generally this depends on your requirement that if you are having custom view called with a tableview as subview then loadview with the initwithframe method will be better and while you want to initialize it from viewcontroller then viewdidload is better. I think you should bifurcate your requirement. Hope this helps.
Since you are initializing table view with zero frame it wont be visible even if you added that
you can try this
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.backgroundColor = kViewBackgroundColor;
UITableView *_tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStyleGrouped];
_tableView.backgroundView = nil;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.bounces = NO;
[_tableView setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth]
[self addSubview:_tableView];
}
return self;
}
If you'd like to create subviews programmatically based on superview metrics (frame or bounds), you should do it in viewDidLoad, because if you do in in initializer, metrics of self.view will not be avaliable, and you subviews will be created with zero frame and will not be visible (as Johnykutty) mentioned above.
But that does not seem to be a good practice in case your subview initialization routines require heavy operations.
Offtopic: I've been codin' for a looong time trying to do everything programmatically, never using xibs, etc. Well, if you ask me now, xibs really save your time and make life easier. Consider using interface builder for layout\autolayout, colors\borders etc stuff.
I am wanting to create a custom UIView class that will show a dynamic number of UISegmentedControl objects depending on some input. For example, if a client has 5 products in their cart, the UIView should generate 5 UISegmentedControl objects that I will then link with each item.
The problem I am having is getting this to work in a UIView. Here is what I have done so far. I am successfully able to create a UISegmentedControl object and display it programmatically within my main UIViewController. I don't get any display when adding it to my UIView class. Here is the implementation code for the UIView class:
#import "ajdSegmentView.h"
#implementation ajdSegmentView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *itemArray = [NSArray arrayWithObjects:#"Yes", #"No", nil];
UISegmentedControl *button = [[UISegmentedControl alloc] initWithItems:itemArray];
button.frame = CGRectMake(35,44, 120,44);
button.segmentedControlStyle = UISegmentedControlStylePlain;
button.selectedSegmentIndex = 1;
[self addSubview:button];
}
return self;
}
#end
I created a new UIView object via Storyboard and placed it inside the UIViewController scene. I made sure to set the class from the generic UIView class to my new custom class. I added and outlet for the UIView in my UIViewController class. Here is the code inside the implementation of UIViewController:
#import "ajdViewController.h"
#interface ajdViewController ()
#end
#implementation ajdViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.segmentView = [[ajdSegmentView alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
That's all I have tried. I have been searching through a lot of pages and trying to implement this without asking here, but I seem to be looking in the wrong places.
First you need to check ajdSegmentView is UIVIew or UIViewController. It is fine if it is UIView. If it is type of UIViewController then you need to add this line while adding Segment.
[self.view addSubview:button];
In place of:
[self addSubview:button];
And One more thing You forget to add this View to your main after allocating so You can declare like this:
objajdSegmentView = [[ajdSegmentView alloc] init];
[self.view addSubview:objajdSegmentView.view];
I have just added this thing. i got result like this way.
Hope this will work for you.
You're initializing your custom view using the init method, but your initialization for ajdSegmentView is in your initWithFrame: method (which in your case is not getting called).
So replace:
self.segmentView = [[ajdSegmentView alloc] init];
with:
// Change the frame to what you want
self.segmentView = [[ajdSegmentView alloc] initWithFrame:CGRectMake(0,0,100,40)];
Also don't forget to add your view to the view controller's view also.
[self.view addSubview:self.segmentView];
Unless this view is being created with interface builder, in which case you will need to override initWithCoder: in your ajdSegmentView class.
I'm not familiar with Storyboard though, so maybe I'm missing something, but in a standard scenario what I said above will solve your problem.
I am trying to animate a UIView (block-based animation) but I cannot make the timing work (duration, etc.). The animation runs, because the changes are done, and the completition output message is shown, but it just ignores the duration and instantly makes the changes.
I asume there is something wrong with the way I am doing things, but I just cannot figure out where the mistake is. Let me explain what I am trying to do:
I have a UIViewController (viewcontroller) that handles two objects (view1 and view2) of a UIView sublcass in which I have defined the touchesEnded method.
The idea is that when view1 has been touched, I want to animate view2. So what I did was to implement an animation method in the UIView subclass, a notification in the touchesEnded method (also in the UIView subclass), and
a trigger method in the controller that would call the animation of the second view. Something like this:
// In the UIView subclass:
- (void) myAnimation{
[UIView animateWithDuration:2.0
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{
self.alpha = 0.5;
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
}
// In the UIView subclass:
- (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event {
[pParent viewHasBeenTouched]; // pParent is a reference to the controller
}
// In the UIViewController:
- (void) viewHasBeenTouched {
[view2 myAnimation];
}
(The animation and work flow is in fact a bit more complex, but this simple example just fails to work)
If I place the animation somewhere else, it may work, for example just after initializing the views in the init method of the controller. But if I try to do this forward-backward calls, the animation will just ignore the duration and execute in one step.
Any ideas? What have I missed about touches and gestures that I should know? Thank you.
NEW INFORMATION ADDED TO THE ORIGINAL POST:
The AppDelegate does nothing but this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[TestViewController alloc] init];
self.window.rootViewController = self.viewController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
The TestViewControler.h is just this:
#import <UIKit/UIKit.h>
#import "TestView.h"
#interface TestViewController : UIViewController{
TestView* myView;
}
- (void) viewHasBeenTouched;
#end
and the viewDidLoad only does this:
- (void)viewDidLoad{
[super viewDidLoad];
myView = [[TestView alloc] initWithParent:self];
[self.view addSubview:myView];
}
Finally, the UIView has a TestView.h which looks like this:
#import <UIKit/UIKit.h>
#class TestViewController; // Forward declaration
#interface TestView : UIView{
TestViewController* pParent;
}
- (id)initWithParent:(TestViewController*) parent;
- (void) myAnimation;
#end
And the init method used is:
- (id)initWithParent:(TestViewController *)parent{
CGRect frame = CGRectMake(50, 20, 100, 200);
self = [super initWithFrame:frame];
if (self) pParent = parent;
self.backgroundColor = [UIColor blueColor];
return self;
}
So... with this simple code I posted, the error occurs. As I said, the animation does change the alpha, but with no delay or timing. It's just instantaneous. Any ideas with this added information?
Thank you again for the help.
I found the problem. I want to apologize first for all the people how have studied my problem and have lost valuable time on it.
All the second part I posted in the original question was copy and pasted indeed, it was the first part that had those two mismatching errors because they came from a more complex code. And that code works without problem as you have all stated. The problem is that I did not copy and paste a method that I thought I had removed from the project (and which is called at the start of the application):
- (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation {
// ... More code
[UIView setAnimationsEnabled:NO];
// ... More code
return YES;
}
It obviously needs no further explanation.
Again thank you very much for your time, and my sincere apologizes.
I know this is really basic stuff but i need to understand whether my understanding of this is correct.
So what i want to do is this. I want an view with a label on which when double tapped flips and loads another view. On the second view i want a UIPickerView and above i have a button saying back. Both views will be of same size as an UIPickerView which is 320px x 216px.
What i am thinking of to do is create two UIViewclasses named labelView and pickerView. I would then create a viewController which on loadView loads labelView then when user double taps the labelView i get an event in labelView class which is sent to my viewController that then can unload loadView and load the pickerView.
Does this sound as the best way to do this ? Is there a simpler way ? I am also unsure how i route the event from the labelView class to the viewControllerclass.
I dont exactly know the most efficient way to do it(as i am also now to this language),but it is for sure that i have solved ur problem. I made a simple program for that.Three classes involved here in my eg are BaseViewController (which will show two views),LabelView and PickerView (according to ur requirement).
In LabelView.h
#protocol LabelViewDelegate
-(void)didTapTwiceLabelView;
#end
#interface LabelView : UIView {
id <LabelViewDelegate> delegate;
}
#property(nonatomic,retain)id <LabelViewDelegate> delegate;
-(void)didTouch;
#end
In LabelView.m
#synthesize delegate;
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self)
{
UILabel* labl = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, frame.size.width-20,20)];
labl.text = #"Some Text";
[self addSubview:labl];
[labl release]; labl = nil;
self.backgroundColor = [UIColor grayColor];
UITapGestureRecognizer* ges = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(didTouch)] autorelease];
ges.numberOfTapsRequired = 2;
[self addGestureRecognizer:ges];
}
return self;
}
-(void)didTouch
{
[delegate didTapTwiceLabelView];
}
//=============================================================
In Pickerview.h
#protocol PickerViewDelegate
-(void)didTapBackButton;
#end
#interface PickerView : UIView <UIPickerViewDelegate,UIPickerViewDataSource>{
id <PickerViewDelegate> delegate;
}
#property(nonatomic,retain)id <PickerViewDelegate> delegate;
#end
In Pickerview.m
#implementation PickerView
#synthesize delegate;
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self)
{
UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 30, 320, 216)];
picker.delegate = self;
picker.dataSource = self;
[self addSubview:picker];
[picker release]; picker = nil;
self.frame = CGRectMake(frame.origin.x, frame.origin.y, 320, 250);
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(10, 1, 50, 27)];
[btn setTitle:#"Back" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(backButton) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
return self;
}
-(void)backButton
{
[delegate didTapBackButton];
}
//====================================================================
in BaseViewController.h
#import "LabelView.h"
#import "PickerView.h"
#interface VarticalLabel : UIViewController<UITextFieldDelegate,PickerViewDelegate,LabelViewDelegate> {
PickerView* myPickerView;
LabelView* myLabelView;
}
#end
In BaseViewController.m
-(void)viewDidLoad
{
[super viewDidLoad];
myPickerView= [[PickerView alloc] initWithFrame:CGRectMake(0, 50, 320, 250)];
[self.view addSubview:myPickerView];
myPickerView.delegate = self;
myLabelView= [[LabelView alloc] initWithFrame:CGRectMake(0, 50, 320, 250)];
[self.view addSubview:myLabelView];
myLabelView.delegate = self;
myPickerView.hidden = YES;
}
#pragma mark PickerViewDelgate
-(void)didTapBackButton
{
myPickerView.hidden = YES;
myLabelView.hidden = NO;
}
#pragma mark LabelViewDelegate
-(void)didTapTwiceLabelView
{
myPickerView.hidden = NO;
myLabelView.hidden = YES;
}
To get events from a button to the view controller, just hook up the button's event, e.g. touch up inside, to a method in the view controller, using interface builder. (Double tapping is probably more complicated though.)
When you say 'flips', do you mean it actually shows an animation of flipping over a view to show a 'reverse' side? Like in the weather app when you hit the 'i' button? I'm assuming this is what you mean.
Perhaps check TheElements sample example on the iPhone Reference Library, it has an example of flip animation.
Btw, it's not strictly necessary to unload the loadView that is being 'hidden' when you flip -- it saves you having to construct it again when you flip back -- but it may be pertinent if you have memory use concerns, and/or the system warns you about memory being low.
Also, what do you mean by "create a UIView"? Do you mean subclass UIView, or just instantiate a UIVIew and add children view objects to it? The latter is the usual strategy. Don't subclass UIView just because you want to add some things to a UIView.
If you've got one screen of information that gives way to another screen of information, you'd normally make them separate view controllers. So in your case you'd have one view controller with the label and upon receiving the input you want, you'd switch to the view controller composed of the UIPickerView and the button.
Supposing you use Interface Builder, you would probably have a top level XIB (which the normal project templates will have provided) that defines the app delegate and contains a reference to the initial view controller in a separate XIB (also supplied). In the separate XIB you'd probably want to add another view controller by reference (so, put it in, give it the class name but indicate that its description is contained in another file) and in that view controller put in the picker view and the button.
The point of loadView, as separate from the normal class init, is to facilitate naming and linking to an instance in one XIB while having the layout defined in another. View controllers are alloced and inited when something that has a reference to them is alloced and inited. But the view is only loaded when it is going to be presented, and may be unloaded and reloaded while the app is running (though not while it is showing). Generally speaking, views will be loaded when needed and unnecessary views will be unloaded upon a low memory warning. That's all automatic, even if you don't put anything in the XIBs and just create a view programmatically within loadView or as a result of viewDidLoad.
I've made that all sound more complicated than your solution, but it's actually simpler because of the amount you can do in Interface Builder, once you're past the curve of learning it. It may actually be worth jumping straight to the Xcode 4 beta, as it shakes things up quite a lot in this area and sites have reported that a gold master was seeded at one point, so is likely to become the official thing very soon.
With respect to catching the double tap, the easiest thing is a UITapGestureRecognizer (see here). You'd do something like:
// create a tap gesture recogniser, tell it to send events to this instance
// of this class, and to send them via the 'handleGesture:' message, which
// we'll implement below...
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleGesture:)];
// we want double taps
tapGestureRecognizer.numberOfTapsRequired = 2;
// attach the gesture recogniser to the view we want to catch taps on
[labelView addGestureRecognizer:tapGestureRecognizer];
// we have an owning reference to the recogniser but have now given it to
// the label. We don't intend to talk to it again without being prompted,
// so should relinquish ownership
[tapGestureRecognizer release];
/* ... elsewhere ... */
// the method we've nominated to receive gesture events
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
// could check 'gestureRecognizer' against tapGestureRecognizer above if
// we set the same message for multiple recognisers
// just make sure we're getting this because the gesture occurred
if(gestureRecognizer.state == UIGestureRecognizerStateRecognized)
{
// do something to present the other view
}
}
Gesture recognisers are available as of iOS 3.2 (which was for iPad only; so iOS 4.0 on iPhone and iPod Touch).