I'm looking for an example of how to create an animated row menu like the Facebook & twitter apps for iphone have. I see the TTTableViewController has the showMenu:forCell: method, but I have not been able to find any examples of how to use it. Specifically in the context of a URL Navigator selector, but any example would be great.
Were you able to find an example on this.
I, too, was stuck with the same problem as you did. I couldn't find an example of using TTTableViewController showMenu:forCell: method a week ago. After messing with the code, I came up with this...
Create a subclass of TTTableViewCell and add a UIButton (as a trigger to launch the menu view) to the view.
#interface MyViewCell : TTTableViewCell {
}
- (id) initWithName:(NSString *)name target:(id)target action:(SEL)action {
// ...
UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
moreButton.frame = CGRectMake(268.0f, 6.0f, 32.0f, 32.0f);
[moreButton setImage:TTIMAGE(#"bundle://Icon_More.png")
forState:UIControlStateNormal];
[moreButton addTarget:target action:action
forControlEvents:UIControlEventTouchUpInside];
[self moreButton];
// ...
}
Next, create a subclass of TTTableViewController and add custom TTTableViewCells to the data source.
#interface MyTableViewController : TTTableViewController {
}
- (void) createModel {
self.dataSource = [TTListDataSource dataSourceWithObjects:
[[[ContactViewCell alloc] initWithName:#"Cell 1"
target:self
action:#selector(moreButtonDidPress:)]
autorelease],
[[[ContactViewCell alloc] initWithName:#"Cell 2"
target:self
action:#selector(moreButtonDidPress:)]
autorelease],
nil];
}
In the action handler, that's where showMenu:forCell: is called. The trick is to determine which cell the button belongs to and consequently replace that cell with the menu view. This is what I did.
- (void) moreButtonDidPress:(id)sender {
// Load our custom menu view from a nib.
UIView *menuView = [[UIView alloc] initWithFrame:cell.contentView];
UIButton *moreButton = (UIButton *) sender;
// Convert plusButton bounds to the the coordinate system of table view
// and then get the cell containing the button.
CGRect coord = [plusButton convertRect:moreButton.bounds toView:self.tableView];
NSIndexPath *path = [self.tableView indexPathForRowAtPoint:coord.origin];
TTTableViewCell* cell = (TTTableViewCell*) [self.tableView
cellForRowAtIndexPath:path];
// Now call showMenu with the menu to display on the associated cell.
[self showMenu:menuView forCell:cell animated:YES];
}
It's not exactly using a URL Navigator selector, but it's functional.
Related
I am hoping to "automate" a click on the segmentController with the index of 0.
My tabBar-based app has multiple segmentControllers in a tab in the ViewDidAppear method, I would like to automatically have it "click" the first segmented controller.
if (segmentController.selectedSegmentIndex == 0) {
//stuff here
}
if (segmentController.selectedSegmentIndex == 1) {
//stuff here
}
Does anyone know how I might accomplish this? Thank you!
If you're creating it programmatically, you could lazy load it like this:
#interface ExampleViewController : UIViewController
#property (nonatomic, strong) UISegmentedControl *segmentedControl;
- (void)segmentedControlClicked:(UISegmentedControl *)segmentedControl;
#end
#implementation ExampleViewController
- (UISegmentedControl *)segmentedControl
{
if (!_segmentedControl)
{
NSArray *items = #[#"First", #"Second", #"Third"];
_segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
[_segmentedControl addTarget:self
action:#selector(segmentedControlClicked:)
forControlEvents:UIControlEventValueChanged];
[_segmentedControl setSelectedSegmentIndex:0]; // Set Default selection
CGRect frame = _segmentedControl.frame;
frame.origin = CGPointMake(0.0f, 0.0f); // Move to wherever you need it
[self.view addSubview:_segmentedControl];
}
return _segmentedControl;
}
- (void)segmentedControlClicked:(UISegmentedControl *)segmentedControl
{
// Whatever your code is goes here...
}
#end
If you're wanting a method to be called also initially, you can call it within your viewDidLoad: method as such:
- (void)viewDidLoad
{
[self.segmentedControl setSelectedSegmentIndex:0]; // Set desired default index (optional if set in lazy load as shown above)
[self segmentedControlClicked:self.segmentedControl];
}
This would hence simulate a click on desired default index.
Be careful putting the above into viewDidAppear: (you could if you really wanted to) because anytime the view comes to the front, this method will be called (in example, if this view controller presents a modal view controller, once the modal is dismissed, this view controller's viewDidAppear: method will be called).
Cheers!
Set the selectedSegmentIndex property on your UISegmentedControl in your viewDidAppear (or viewDidLoad) method.
self.segmentedController.selectedSegemntIndex = 1;
UISegmentedControl Reference
I've got a QuickLook view that I view some of my app's documents in. It works fine, but I'm having my share of trouble closing the view again. How do I create a touch event / gesture recognizer for which I can detect when the user wants to close the view?
I tried the following, but no events seem to trigger when I test it.
/------------------------ [ TouchPreviewController.h ]---------------------------
#import <Quicklook/Quicklook.h>
#interface TouchPreviewController : QLPreviewController
#end
//------------------------ [ TouchPreviewController.m ]---------------------------
#import "TouchPreviewController.h"
#implementation TouchPreviewController
- (id)init:(CGRect)aRect {
if (self = [super init]) {
// We set it here directly for convenience
// As by default for a UIImageView it is set to NO
UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleDoubleTap:)];
singleFingerDTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:singleFingerDTap];
[self.view setUserInteractionEnabled:YES];
[self.view setMultipleTouchEnabled:YES];
//[singleFingerDTap release];
}
return self;
}
- (IBAction)handleSingleDoubleTap:(UIGestureRecognizer *) sender {
CGPoint tapPoint = [sender locationInView:sender.view.superview];
[UIView beginAnimations:nil context:NULL];
sender.view.center = tapPoint;
[UIView commitAnimations];
NSLog(#"TouchPreviewController tap!" ) ;
}
// I also tried adding this
- (BOOL)gestureRecognizer:(UIGestureRecognizer *) gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*) otherGestureRecognizer {
return YES;
}
#end
Edit: For clarification, this is how I instantiate the controller:
documents = [[NSArray alloc] initWithObjects: filename , nil ] ;
preview = [[TouchPreviewController alloc] init];
preview.dataSource = self;
preview.delegate = self;
//set the frame from the parent view
CGFloat w= backgroundViewHolder.frame.size.width;
CGFloat h= backgroundViewHolder.frame.size.height;
preview.view.frame = CGRectMake(0, 0,w, h);
//refresh the preview controller
[preview reloadData];
[[preview view] setNeedsLayout];
[[preview view] setNeedsDisplay];
[preview refreshCurrentPreviewItem];
//add it
[quickLookView addSubview:preview.view];
Also, I've defined the callback methods as this:
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
return [documents count];
}
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index
{
return [NSURL fileURLWithPath:[documents objectAtIndex:index]];
}
Edit2: One thing i noticed. If I try making swiping gestures, I get the following message. This could shed some light on what is wrong/missing?
Ignoring call to [UIPanGestureRecognizer setTranslation:inView:] since
gesture recognizer is not active.
I think your example code is incomplete. It isn't clear how you are instantiating the TouchPreviewController (storyboard, nib file or loadView.)
I have never used the class so I could be way out in left field.
If you've already instantiated a UITapGestureRecognizer in the parent viewController, it is absorbing the tap events and they aren't passed on to your TouchPreviewController.
I would implement the view hierarchy differently by attaching the UITapGestureRecognizer to the parent viewController and handle presentation and unloading of the QLPreviewController there.
I think you might not have to subclass QLPreviewController by instantiating the viewController from a nib file.
When your parent viewController's UITapGestureRecognizer got an event you would either push the QLPreviewController on the navigation stack or pop it off the navigation stack when done.
Hope this is of some help.
I'm new to iOS development. To keep my iOS app nicely compartmentalised I'd like to create both the UIView and the UIViewController programatically, and tie them together once created.
So, I do the following: in my view controller I have this:
-(void)loadView {
NSLog(#"HPSMainMenuViewController loadView starting");
HPSMainMenuView* mainmenuView = [[HPSMainMenuView alloc]initWithFrame:CGRectZero];
self.view = mainmenuView;
}
and in my View I have this:
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSLog(#"HPSMainMenuView initWithFrame starting");
[self setup];
}
return self;
}
-(void)setup {
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.tag = E_PROFILE_BUTTON;
[btn setTitle:#"Option1" forState:UIControlStateNormal];
[self.view addSubview:btn ];
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.tag = E_CONTACTS_BUTTON;
[btn setTitle:#"Option2" forState:UIControlStateNormal];
[self.view addSubview:btn ];
self.title = #"Hello";
}
Is this the right way to do this (given I want full programmatic control). It seems wrong to dynamically build the view within the ViewController hence my approach where I am building the view within an actual UIView class.
Lastly, I'm using loadView; should I be using viewDidLoad? If so, why?
Thanks very much.
What you are doing is correct, and actually good. Many developers keep their view controllers and views heavily tied together but if you want to keep them separate then that's great.
loadView is where you should be creating and initializing everything. viewDidLoad can be called multiple times if the view is unloaded/reloaded due to memory warnings (for example). So viewDidLoad is where you would restore saved state, make your view correctly reflect your model, or any other initialization that you can't do in loadView.
I have a toolbar and i would like to place it on top of the keyboard.
In the keyboardwillshow notification, i tried to add toolbar to the keyboard but no luck, i cant add
Please let me know
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
//Get a reference of the current view
keyboard = [tempWindow.subviews objectAtIndex:i];
//Check to see if the description of the view we have referenced is "UIKeyboard" if so then we found
//the keyboard view that we were looking for
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES)
{
[keyboard addSubview:myToolbar];
}
}
I think, you want an inputAccessoryView.
Basically, you create a view and set it as a text field's or text view's input accessory view.
[textField setInputAccessoryView:inputAccessoryView];
Here is code in case someone else need it.Found on stack overflow
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Clear" style:UIBarButtonItemStyleBordered target:self action:#selector(clearNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Apply" style:UIBarButtonItemStyleDone target:self action:#selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
numberTextField.inputAccessoryView = numberToolbar;
}
-(void)clearNumberPad{
[numberTextField resignFirstResponder];
numberTextField.text = #"";
}
-(void)doneWithNumberPad{
NSString *numberFromTheKeyboard = numberTextField.text;
[numberTextField resignFirstResponder];
}
https://github.com/asefnoor/IQKeyboardManager
This is the best keyboard handler I have seen. Very excellent way to manage Text inputs.
Some of its features 1) ZERO LINE OF CODE
2) Works Automatically
3) No More UIScrollView
4) No More Subclasses
5) No More Manual Work
6) No More #imports
Simple solution for iOS 8 and 9.
Init your textField, searchBar etc.
customView - View that will stick at the top of the keyboard. DO not add it as a subview to hierarchy! Do not have to set any constraints or positions.
[searchBar setInputAccessoryView:self.customView];
- (BOOL)canBecomeFirstResponder{
return true;
}
- (UIView *)inputAccessoryView {
return self.customView;
}
If you need to let the customView at the bottom and not hide it after dismissing keyboard, add observer:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:self.view.window];
And method
- (void)keyboardDidHide:(NSNotification *)notification {
[self becomeFirstResponder];
}
if you want to add tool bar on the keyboard, you are right place. You can use BSKeyboard easly. Look at the implementation below;
in .h file
#import "BSKeyboardControls.h"
add delegates
#interface ViewController : UIViewController <BSKeyboardControlsDelegate>
now jump the .m file , go to viewDidLoad. We will add textfield which i want to add tollbar
self.keyboardControls = [[BSKeyboardControls alloc] initWithFields:#[PINTextField]];
[self.keyboardControls addDelegate:self];
we should add activeField just like below,
- (void)textFieldDidBeginEditing:(UIView *)textField {
[self.keyboardControls setActiveField:textField];
}
And last step is delegate metods implemantation,
- (void)keyboardControlsDonePressed:(BSKeyboardControls *)keyboardControls {
[super textFieldShouldReturn:self.keyboardControls.activeField];
}
That's it.
For more info and download the BSKeyboard files, you can move the below link BSKeyboard Githup Link
I have an interface that has an NSTextField, NSButton, and an NSView. When I type something in the NSTextfield and press the button, I want the text to be drawn in the NSView. So far I have everything connected and working, except for the view.
How can I connect the text and the view so that every time I press the button, the text is drawn to the view?
How can I connect the text and the view so that every time I press the button, the text is drawn to the view?
Views do their own drawing.
You need to give the view the string to draw, and then set the view as needing display. You'll do these in the action method that you wire the button up to.
First, your custom view class needs to have a property for the value (string, in this case) that it's going to display. From your action method, which should generally be on a controller object, send the view object a setFoo: message (assuming you named the property foo). That takes care of job one: The view now has the value to display.
Job two is even easier: Send the view a setNeedsDisplay: message, with the value YES.
That's it. The action method is two lines.
Of course, since views draw themselves, you also need your custom view to actually draw, so you need to implement the drawRect: method in that class. It, too, will be short; all you need to do is tell the string to draw itself.
Bindings
http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/CocoaBindings/Concepts/WhatAreBindings.html#//apple_ref/doc/uid/20002372-CJBEJBHH
For simplicity's sake I didn't mention this before, but the app also has a speech element to speak the string. This aspect of the program works fine, so just ignore any messages involving the SpeakAndDraw class (it's actually misnamed and only includes a speech method, nothing about drawing).
View.m
#import "View.h"
#implementation View
#synthesize stringToDraw;
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setAttributes];
stringToDraw = #"Hola";
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
NSRect bounds = [self bounds];
[self drawStringInRect:bounds];
}
- (void)setAttributes
{
attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:[NSFont fontWithName:#"Helvetica"
size:75]
forKey:NSFontAttributeName];
[attributes setObject:[NSColor blackColor]
forKey:NSForegroundColorAttributeName];
}
- (void)drawStringInRect:(NSRect)rect
{
NSSize strSize = [stringToDraw sizeWithAttributes:attributes];
NSPoint strOrigin;
strOrigin.x = rect.origin.x + (rect.size.width - strSize.width)/2;
strOrigin.y = rect.origin.y + (rect.size.height - strSize.height)/2;
[stringToDraw drawAtPoint:strOrigin withAttributes:attributes];
}
#end
SpeakerController.m
#import "SpeakerController.h"
#implementation SpeakerController
- (id)init
{
speakAndDraw = [[SpeakAndDraw alloc] init];
view = [[View alloc] init];
[mainWindow setContentView:mainContentView];
[mainContentView addSubview:view];
return self;
}
- (IBAction)speakText:(id)sender
{
[speakAndDraw setStringToSay:[text stringValue]];
[speakAndDraw speak];
[view setStringToDraw:[text stringValue]];
[view setNeedsDisplay:YES];
NSLog(#"%#", view.stringToDraw);
NSLog(#"%#", [view window]);
}
#end