i used this code to detect touch on an uiscrollview who is on top of uiview
subclass
.h file
#interface AppScrollView : UIScrollView
{
}
#end
.m file
#import "AppScrollView.h"
#implementation AppScrollView
- (id)initWithFrame:(CGRect)frame
{
return [super initWithFrame:frame];
}
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
// If not dragging, send event to next responder
if (!self.dragging)
[self.nextResponder touchesEnded: touches withEvent:event];
else
[super touchesEnded: touches withEvent: event];
}
#end
and then on another class i added
#import <UIKit/UIKit.h>
#class AppScrollView;
#interface SomeClass : UIViewController <UIScrollViewDelegate>
{
AppScrollView *scrollView;
...
}
#end
#import "AppScrollView.h"
#implementation SomeClass
...
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
// Process the single tap here
...
}
...
#end
i used also scroll1.delegate=self; but nothing happens!!!
Can anyone help me?
Use Gesture Recognizers. Specifically UITapGestureRecognizer.
Related
I am trying to make an application that counts the number of clicks on a button, and plays a sound when the number of clicks is 10 ... I've tried a lot of different code, but it's not working. Any help is appreciated.
This is the .m file
What should I add inside the if statement?
#import "ViewController.h"
#import "AVFoundation/AVAudioPlayer.h"
#interface ViewController ()
#end
#implementation ViewController
-(IBAction)Add{
count++;
number.text =[NSString stringWithFormat:#"%i", count];
if (count==10) {
}
}
-(IBAction)Reset{
count=0;
number.text =[NSString stringWithFormat:#"%i", count];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And this is the .h file
#import <UIKit/UIKit.h>
int count;
#interface ViewController : UIViewController
{
IBOutlet UILabel *number;
}
-(IBAction)Add;
-(IBAction)Reset;
#end
I am using core data and am trying to add table view cells to a table view in a view controller. When I implement it in a table view controller the app runs. When I run it when the table view is part of another view controller, the app does not run. It shows an error in the code: property tableview not found on object of type DeviceViewController. DeviceViewController.h has a table view data source and delegate. My code is:
#import "DeviceViewController.h"
#import "DeviceDetailViewController.h"
#interface DeviceViewController ()
#property (strong) NSMutableArray *devices;
#end
#implementation DeviceViewController
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:#selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Fetch the devices from persistent data store
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:#"Device"];
self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.devices.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSManagedObject *device = [self.devices objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:#"%# ", [device valueForKey:#"name"]]];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[context deleteObject:[self.devices objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(#"Can't Delete! %# %#", error, [error localizedDescription]);
return;
}
// Remove device from table view
[self.devices removeObjectAtIndex:indexPath.row];
[self. otableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
As I see it, in this case, it has nothing to do with CoreData.
You should add the UITableViewDataSource and UITableViewDelegate protocols to your VC.
After that you should create a UITableView (you can do it in interface builder) and connect the datasource and delegate to your view controller.
You can also add a property for the UITableView in your VC.
When you use UITableViewController, all this is done from Xcode, but if you use UITableView inside a regular UIViewController you should take care of this stuff.
Good Luck!
EDIT:
Add this as a propert
#property (nonatomic, strong) UITableView *deviceTableView;
Change the viewDidLoad method to this:
- (void)viewDidLoad {
[super viewDidLoad];
_deviceTableView = [[UITableView alloc] initWithFrame:self.view.frame];
_deviceTableView.delegate = self;
_deviceTableView.dataSource = self;
[self.view addSubview:_deviceTableView];
[_deviceTableView reloadData];
}
Delete what you have written in viewDidAppear and replace self.tableView with _deviceTableView everywhere in this file.
Another problem that could be causing the crashes is if your NSManagedObjectContext is nil when you are trying to execute the fetch request. I would add an if([self managedObjectContext])
before using it in any of your view hierarchy methods.
Also, you should be careful when using views instantiated from storyboards. Nikola's answer said to add the code configuring the view controller in viewDidLoad, and that is ok as long as you did not drag a UITableView onto your UIViewController instance in interface builder. If you already have created and linked a tableview in interface builder, you will be needlessly replacing it with a new one, so before the
_deviceTableView = [[UITableView alloc] initWithFrame:self.view.frame];
line, you should add an if(!_deviceTableView)
statement because it will prevent you from adding a second table view to your view. The issue is that if you have already added a tableview in IB, it will be retained by your view even after you change your tableview property, so it is possible to have two tables show up.
Alternatively, you could use "lazy instantiation" in your getter method for the table view property to make sure it is initialized when you access it.
Lastly, in order to help give us a better idea of why your app is crashing it would be helpful to get the exception log from the console in Xcode.
On iPhone, in Xcode, I can show a popup view which overlays everything, including the Tab Bar, etc, by using code like this -
[[[[UIApplication sharedApplication] delegate] window] addSubview:mySpecialView];
I'm trying to do the same in MonoTouch, and the code I'm using is this -
UIApplication.SharedApplication.Delegate.Window.AddSubview(mySpecialView);
...but this crashes. Does anyone have any idea what I'm doing wrong?
Thanks for any help.
You did not say how it crashed - but I assume you're having a ModelNotImplementedException while using the Window property since it's not implemented by default (and is meant for storyboard).
You can either implement it to return the window field of the (autogenerated) AppDelegate (AppDelegate.cs file) or expose the same variable as a (static) field.
E.g. the default generated code
UIWindow window;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
window.RootViewController = new UINavigationController ();
window.MakeKeyAndVisible ();
return true;
}
would become:
static UIWindow window;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
window.RootViewController = new UINavigationController ();
window.MakeKeyAndVisible ();
return true;
}
static public UIWindow Window {
get { return window; }
}
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am new to iOS, i am using Xcode 4.3.2, and i have created a project with tabView, where by default it has two view controllers. I am creating a new one now,
ThirdView.h
#import <UIKit/UIKit.h>
#interface ThirdView : UIViewController
#end
#import "ThirdView.h"
#implementation ThirdView
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Third", #"Third");
self.tabBarItem.image = [UIImage imageNamed:#"Third"];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
and in the appDelicate.m
viewController3 = [[ThirdView alloc] initWithNibName:#"ThirdView" bundle:nil];
and added viewController3 to tabBarController.
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3 ,nil];
The Problem is:
When i execute the code in the device, i could see the third tab, but the moment i click, it crashes.
Where i am doing wrong ?
Double check the class name of the third view controller in you nib file.
My application is in landscape mod.When i call MFMailComposeViewController through Present model view, it comes in landscape mod.I rotate device and MFMailComposeViewController view goes to portrait mod i want to restrict this rotation it should always be in landscape mod only.Is there any way to do it..
Subclass the MFMailComposeViewController class, so that you can override its shouldAutorotateToInterfaceOrientation to display it however you like:
#interface MailCompose : MFMailComposeViewController {
}
#end
#implementation MailCompose
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
#end
Specify the new class in place of MFMailComposeViewController:
MailCompose *controller = [[MailCompose alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:#"In app email..."];
[controller setMessageBody:#"...email body." isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];
I found that a simple category for MFMailComposeViewController also worked. Since I like my app to rotate to any angle, I created & linked in MFMailComposeViewController+Rotate.h:
#import <Foundation/Foundation.h>
#import <MessageUI/MFMailComposeViewController.h>
#interface MFMailComposeViewController (Rotate)
#end
and MFMailComposeViewController+Rotate.m
#import "MFMailComposeViewController+Rotate.h"
#implementation MFMailComposeViewController (Rotate)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
// return the desired orientation mask from http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html
return /*mask*/;
}
#end
For my base line testing purposes (iOS 3.1.3), I don't seem to need to put it into the root view controller.
It is worth noting that BEFORE I did this, after my app would load MFMailComposeViewController, it would then stop rotating to any other orientation, even after MFMailComposeViewController was dismissed! Now my app remains freely rotatable.
Henry
This worked for me, perfectly, with one minor change:
MailCompose *controller = [[MailCompose alloc]
initWithRootViewController:self.navigationController];
...
And I was convinced that I had to call the base-class initWithRootViewController method. Otherwise, how would the MFMailComposeViewController know about it? But it turns out the example above that simply calls [[MailCompose alloc] init] is enough. The underlying MFMailComposeViewController "just knows" how to display itself.
I love happy surprises like this. I posted this in case it is equally illuminating for anyone else.
Create a new controller and inherit it from MFMailComposeViewController.In this controller just write one function shouldautorotate thing. Create instance of this.Now it will be working fine.