How to correctly detect touches in my UIView? - iphone

I'm having a problem. I have a UIView, that looks like this :
In that view controller I implemented the "touchesBegan:withEvent:" method, but the method is only getting triggered when I touch the bar at the bottom of the view , nothing happens when I touch the table.
How could I change this behavior to be able to detect the touches that occur in the table too ?
This is the code that basically declares that UIViewController:
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#interface TripSearchDetailViewController : UIViewController
{
//Parent of this sub-view.
UIViewController *parentController;
//GUI elements
UITableView *tableView;
UIButton *backButton;
}
//Button actions
- (IBAction) goBack:(id) sender;
- (IBAction) showDatePicker:(id) sender;
//Class Methods.
- (void) presentDatePicker;
#property (nonatomic, retain) UIViewController *parentController;
#property (nonatomic, retain) IBOutlet UITableView *tableView;
#property (nonatomic, retain) IBOutlet UIButton *backButton;
#end

It looks like you've got a UITableViewController, not a UIViewController. Your table view is intercepting and handling touches before they make it up to your view controller. You'll need to subclass UITableView, override it's -touchesBegan:withEvent: method, and then create a standard UIViewController that adds your new subclass to the view hierarchy.

I think it could possibly help to implement the method hitTest:withEvent: in your TripSearchDetailViewController. If you just return true in this method your touch should be recognized.
see also: http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/EventHandling/EventHandling.html

Related

How to add a popup UIView (of a customized class) on a UIViewController

I'm new on this, and I would like to get some advice because I don't know what I'm doing wrong.
I want to make an app in xcode, with a UIView with some items, and when you do something, another UIView (smaller than the first) pops up above the first UIView. The popup UIView would be a customized class.
I have started with the UIViewController template and the initial UIView, and I have linked all the items in the .storyboard, and it works. But when I create my own UIView class (from objective-C class), put the second UIView over the first in the storyboard and link it to my class, something goes wrong.
The UIView appears, but when I try to set it to hidden, it doesn't answer. It's like it's not receiving the messages, so I think I don't link it well programmatically and just appears because of the storyboard.
I don't know if I have to create another UIViewController instead of the UIView, or if this is the correct path.
Can anybody explain me a little, or just write a little code snippet with the instantiation of the second view and adding it?
Lots of thanks!!
(I paste some code, of the declaration in .h and instantiation in .m)
#import <UIKit/UIKit.h>
#import "EditView.h"
#interface ReleaseViewController : UIViewController <UIWebViewDelegate, UISearchBarDelegate> {
IBOutlet UIWebView *web;
IBOutlet UISearchBar *search;
IBOutlet EditView *evHack;
}
#property (nonatomic, retain) IBOutlet UIWebView *web;
#property (nonatomic, retain) IBOutlet UISearchBar *search;
#property (nonatomic, retain) IBOutlet EditView *evHack;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
search.delegate = self;
web.delegate = self;
evHack = [evHack initWithFrame:CGRectMake(0, 44, 320, 377)];
[evHack setHidden:YES];
}
EditView Class (I still have nothing):
#import <UIKit/UIKit.h>
#interface EditView : UIView
#end
#import "EditView.h"
#implementation EditView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(#"View created");
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
initWithFrame only works when you alloc/init an app. If its already initialized, in this case by the storyboard, just set its frame:
evHack.frame = CGRectMake(0,44, 320, 377);
I don't know what it looks like in IB, But setting its frame in code may be redundant if you set it in IB too. To check whether evHack is hooked up right, NSLog evHack in viewDidLoad. If you get nil back, it's not hooked up right.

is not a kind of ... as specified by the outlet type

I'm using a derived UISearchBar because I have to set the font size:
#import <UIKit/UIKit.h>
#interface SearchBar : UISearchBar
{
UITextField *searchField;
}
#property (nonatomic, retain) UITextField *searchField;
- (void) setFontSize: (NSInteger) size;
#end
In my ViewController the Outlet is defined:
#interface DictViewController : UIViewController <UIApplicationDelegate, UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *myTableView;
IBOutlet SearchBar *mySearchBar;
NSMutableArray *searchResults;
}
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
#property (nonatomic, retain) IBOutlet SearchBar *mySearchBar;
...
InterfaceBuilder don't let me connect the SearchBar to mySearchBar.
If I change the outlet class to UISearchBar, IB let me connect to mySearchBar. When I change the class back to SearchBar, the connection remains, but with the warning "The 'mySearchBar' outlet of FilesOwner is connected to 'SearchBar' but 'UISearchBar' is not a kind of 'SearchBar' as specified by the outlet type"
What's wrong? Any idea what I can do?
This might fix the problem.
In IB, select the search bar and go to the Identity pane of the inspector window. In the class dropbox, choose SearchBar instead of UISearchBar.

UIViewController IBOutlets are nil

I have an UIViewController class with two labels and a UIImageView set as IBOutlets, and I have this outlets connected in my xib, I have double checked they are connected properly, however when I check the their value in the debugger they are 0x0 so I cant change them programatically. Any ideas on what I might be doing wrong.
Heres the header file of my code:
#import <UIKit/UIKit.h>
#interface PlateDetailViewController : UIViewController {
IBOutlet UIImageView *image;
IBOutlet UILabel *price;
IBOutlet UILabel *description;
}
#property (nonatomic, retain)IBOutlet UIImageView *image;
#property (nonatomic, retain)IBOutlet UILabel *price;
#property (nonatomic, retain)IBOutlet UILabel *description;
#end
Your outlets won't get set until the view controller's view is actually instantiated, which in your case is probably happening shortly after initWithNibName:bundle:—at which point they'll still be nil. Any setup you do that involves those outlets should be happening in your view controller's -viewDidLoad method.

Is it possible to set property object variable in another class in iphone?

I have a property in my UIView class, something like that:
myView.h
# interface MyView: UIView {
id refView;
}
#property (nonatomic, retain) id refView;
MyView.m
#synthesis refView;
Then in another class can I set the refView property of MyView something like that:
MyViewController.h
#interface MyViewController: UIViewController {
UIView *myView;
}
#property (nonatomic, retain) IBOutlet UIView *myView;
then in MyViewController.m:
[self.myView setRefView:someValue];
When I do that I get this warning: UIView may not response to '-setRefView:' ...
So, is the above way is right or what's the right way to do it. Any help and explanation would be greatly appreciate it. Thanks.
You added the refView property to MyView class, not UIView, that's why the compiler complains. If you initialize myView with a MyView instance, this will work, even if there's a warning.
To prevent the compiler from complaining, change
#interface MyViewController: UIViewController {
UIView *myView;
}
to
#interface MyViewController: UIViewController {
MyView *myView;
}

Passing object from controller to a view

I'm following iPhone dev courses from Stanford Open-University, and I've been blocked for 2 days on assignment3, maybe someone can help me here?
The tasks are:
Create a custom UIView subclass that will display your PolygonShape object
Give your view class access to the PolygonShape object so that it can retrieve the details of the polygon as needed
The problem is: how do I give my view class access to the polygon object defined in my controller?
Here is my implementations if it can help:
CustomView.h:
#import "PolygonShape.h"
#interface CustomView : UIView {
IBOutlet PolygonShape *polygon;
}
- (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides;
#end
Controller.h:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "PolygonShape.h"
#import "PolygonView.h"
#interface Controller : NSObject {
IBOutlet UIButton *decreaseButton;
IBOutlet UIButton *increaseButton;
IBOutlet UILabel *numberOfSidesLabel;
IBOutlet PolygonShape *polygon;
IBOutlet PolygonView *polygonView;
}
- (IBAction)decrease;
- (IBAction)increase;
- (void)awakeFromNib;
- (void)updateInterface;
#end
And after you figure it out, it might not hurt to touch up on some objective-c basics:
http://www.cocoacast.com/?q=node/103
Found my own answer, I missed a setPolygon method in my CustomView to link both... stupid...
in CustomView.h:
#import "PolygonShape.h"
#interface CustomView : UIView {
IBOutlet PolygonShape *polygon;
}
#property (readwrite, assign) PolygonShape *polygon;
- (NSArray *)pointsForPolygonInRect:(CGRect)rect numberOfSides:(int)numberOfSides;
#end
in CustomView.m:
#implementation CustomView
#synthesize polygon;
...
#end
in Controller.m:
- (void)awakeFromNib {
// configure your polygon here
polygon = [[PolygonShape alloc] initWithNumberOfSides:numberOfSidesLabel.text.integerValue minimumNumberOfSides:3 maximumNumberOfSides:12];
[polygonView setPolygon:polygon];
NSLog (#"My polygon: %#", [polygon description]);
}
I just finished assignement 3 last night. I solved this connection all in Interface Builder. First I created an outlet on the "PolygonView" UIView subclass for the PolygonShape and then connected it to the instance of the Polygon model. From what I have read in the Google Group and on various other sites, I do not think there is one right way to connect this UIView to the model and the controller. But it worked I think there is nothing wrong with the View knowing about the model.
So why aren't you declaring them as properties of the class?