*iam a beginner in iphone development.
i have one problem in uitextview...wat iam trying to do is dragging the the selected string in uitextview from out of the textview...and drag it to the tabbarcontroller is it possible plz any on help me on this....*this is the code ihave return up to now....plz help me
#
import <UIKit/UIKit.h>
#import "TabViewController.h"
#class TabBarViewController;
#interface TabBarAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
{
TabBarViewController *txtviewcontroller;
UITabBarController *tabbar;
NSArray *viewcontrollerarray;
}
#property(nonatomic,retain)NSArray *viewcontrollerarray;
#property(nonatomic,strong)UITabBarController *tabbar;
#property(nonatomic,retain)TabBarViewController *txtviewcontroller;
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) TabBarViewController *viewController;
#end
#import "TabBarAppDelegate.h"
#import "TabViewController.h"
#implementation TabBarAppDelegate
#synthesize txtviewcontroller,tabbar,viewcontrollerarray;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor=[UIColor whiteColor];
self.tabbar=[[UITabBarController alloc]init];
txtviewcontroller=[[TabBarViewController alloc]init];
tabbar.delegate=self;
viewcontrollerarray=[[NSArray alloc]initWithObjects:txtviewcontroller, nil];
self.tabbar.viewControllers=viewcontrollerarray;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[TabBarViewController alloc] initWithNibName:#"TabBarViewController_iPhone" bundle:nil];
} else {
self.viewController = [[TabBarViewController alloc] initWithNibName:#"TabBarViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.tabbar;
[self.window makeKeyAndVisible];
return YES;
}
#import <UIKit/UIKit.h>
#interface TabBarViewController : UIViewController
{
UITextView *textview;
}
#property(nonatomic,retain)UITextView *textview;
#end
#import "TabViewController.h"
#import "TabBarAppDelegate.h"
#include <QuartzCore/CoreAnimation.h>
#interface TabBarViewController ()
#end
#implementation TabBarViewController
#synthesize textview;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title=#"firstname";
CGRect textViewFrame = CGRectMake(20.0f, 20.0f, 280.0f, 124.0f);
textview = [[UITextView alloc] initWithFrame:textViewFrame];
textview.backgroundColor=[UIColor clearColor];
textview.textColor=[UIColor blackColor];
textview.editable=NO;
NSString *filePath=[[NSBundle mainBundle]pathForResource:#"satyadetails" ofType:#"txt"];
NSString *contentString=[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
textview.text=contentString;
textview.layer.borderWidth = 3.0f;
textview.layer.borderColor = [[UIColor grayColor] CGColor];
textview.returnKeyType = UIReturnKeyDone;
[self.view addSubview:textview];
enter code here
}
Step 1. Get event when user touches inside textview (You can get that by UITextView's Delegate (startEditing delegate))
Step 2. Add a UILabel on ur view giving position where user touches in textview and giving text as that of textview and giving clear color as backgroundcolor. (Do this inside delegate of textview)
Step 3. Inside touches move of ur view change the positions of ur label dynamically according to touches.
Step 4. When user moves its touches till dropping textview delegate will be called check there if(textview==droppingtextview) then put draggingtextview.text=label.text. and remove label from superview.
Related
I have been stuck here.
I have a NSString in Appdelegate. And I have two views called firstview,second view.In first view I have a label and set text from stringvariable which is in Appdelegate .When I click on the button in first view,It goes to the secondview(added the second view to first view as a subview).In second view I have a back button.when I click the back button it is displaying the first view ( Here I am setting the value which is in the appdelegate.And then used this [self.view removeFromSuperview]).The Problem is first view is appearing but label value is not updating.Can any body tell me how to update the Labeltext.Kindly tell me.
Appdelegate.h
#import <UIKit/UIKit.h>
#class FirstView;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSString *str1;
}
#property (nonatomic,retain) NSString *str1;
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#end
Appdelegate.m
#import "AppDelegate.h"
#import "FirstView.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
#synthesize str1;
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[FirsView alloc] initWithNibName:#"FirstView" bundle:nil] autorelease];
self.str1 = #"view1";
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
FirstView.h
#import <UIKit/UIKit.h>
#import "secondView.h"
#interface FirstView : UIViewController
{
IBOutlet UILabel *btn;
secondView *cont;
}
-(IBAction)gotoView2:(id)sender;
#end
FirstView.m
#import "FirstView.h"
-(IBAction)gotoView2:(id)sender
{
cont = [[secondView alloc] initWithNibName:#"secondView" bundle:nil];
[self.view addSubview:cont.view];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[btn setTitle:del.str1];
}
**SecondView.h**
#import <UIKit/UIKit.h>
#interface SecondView : UIViewController
{
}
-(IBAction)goBack:(id)sender;
#end
SecondView
#import "SecondView.h"
#import "AppDelegate.h"
#implementation SecondView
-(IBAction)gotoView1:(id)sender
{
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[del setStr1:#"Home"];
[self.view removeFromSuperView];
}
There is a pattern how to do such things. You should define a protocol like this:
#protocol FirstViewDelegate <NSObject>
- (void)didDismissSecondView;
#end
Your first view should conform to this protocol:
#interface FirstView: UIViewController <FirstViewDelegate> {
...
in its implementation add function didDismissSecondView:
- (void) didDismissSecondView
{
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[btn setTitle:del.str1];
}
Your second view has to have a property
#interface SecondView : UIViewController
{
id<FirstViewDeledate> delegate;
}
#property (nonatomic, retain) id<FirstViewDeledate> delegate;
When you show your second view from the first view set its delegate to self of first view
in your function:
-(IBAction)gotoView2:(id)sender
{
SecondView *aView = [[SecondView alloc] init] // or whatever
...//other initialization code
aView.delegate = self;
... // show it
}
and before you dismiss the second view:
-(IBAction)gotoView1:(id)sender
{
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[del setStr1:#"Home"];
[self.delegate didDismissSecondView];
[self.view removeFromSuperView];
}
And you done.
A bit long, but works.
In my custom Tab Bar Application, the orientation never seems to change, even if I force rotate the status bar. Here is my code in my AppDelegate:
AppDelegate.h:
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
#class exampleViewContoller;
#class example1ViewController;
#class example2ViewController;
#class example3ViewController;
#class example4ViewController;
#interface <appname>AppDelegate : NSObject <UIApplicationDelegate, MBProgressHUDDelegate> {
UIWindow *window;
UITabBarController *rootController;
exampleViewContoller *viewController;
example1ViewController *viewController1;
example2ViewController *viewController2;
example3ViewController *viewController3;
example4ViewController *viewController4;
NSMutableData *responseData;
NSMutableArray *tweets;
MBProgressHUD *HUD;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#property (nonatomic, retain) IBOutlet exampleViewContoller *viewController;
#property (nonatomic, retain) IBOutlet example1ViewController *viewController1;
#property (nonatomic, retain) IBOutlet example2ViewController *viewController2;
#property (nonatomic, retain) IBOutlet example3ViewController *viewController3;
#property (nonatomic, retain) IBOutlet example4ViewController *viewController4;
#property (nonatomic, retain) NSMutableArray *tweets;
#end
AppDelegate.m:
#import "<appname>AppDelegate.h"
#import "exampleViewContoller.h"
#import "example1ViewController.h"
#import "example2ViewController.h"
#import "example3ViewController.h"
#import "example4ViewController.h"
#import "SBJson.h"
#define TMP NSTemporaryDirectory()
#implementation <appname>AppDelegate
#synthesize window = _window;
#synthesize rootController;
#synthesize viewController;
#synthesize viewController1;
#synthesize viewController2;
#synthesize viewController3;
#synthesize viewController4;
#synthesize tweets;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGFloat width = self.rootController.view.bounds.size.width;
CGFloat height = self.rootController.view.bounds.size.height;
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)];
UIImage *imageView = [UIImage imageNamed:#"TabBarBackground.png"];
UIColor *kMainColor = [[UIColor alloc] initWithPatternImage:imageView];
[v setBackgroundColor:kMainColor];
[kMainColor release];
[self.rootController.tabBar insertSubview:v atIndex:0];
[imageView release];
[v release];
responseData = [[NSMutableData data] retain];
tweets = [NSMutableArray array];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:#"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=ENTER_USER_HERE&count=20"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSAssert(nil != self.rootController, #"tab bar controller not hooked up!");
BOOL iPad = NO;
#ifdef UI_USER_INTERFACE_IDIOM
iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#endif
if (iPad) {
self.viewController = [[[exampleViewContoller alloc] initWithNibName:#"exampleViewController_iPad" bundle:nil] autorelease];
self.viewController1 = [[example1ViewController alloc] initWithNibName:#"example1ViewController_iPad" bundle:nil] autorelease];
self.viewController2 = [[[example2ViewController alloc] initWithNibName:#"example2ViewController_iPad" bundle:nil] autorelease];
self.viewController3 = [[[example3ViewController alloc] initWithNibName:#"example3ViewController_iPad" bundle:nil] autorelease];
self.viewController4 = [[[example4ViewController alloc] initWithNibName:#"example4ViewController_iPad" bundle:nil] autorelease];
} else {
self.viewController = [[[exampleViewContoller alloc] initWithNibName:#"exampleViewContoller_iPhone" bundle:nil] autorelease];
self.viewController1 = [[[example1ViewController alloc] initWithNibName:#"example1ViewController_iPhone" bundle:nil] autorelease];
self.viewController2 = [[[example2ViewController alloc] initWithNibName:#"example2ViewController2_iPhone" bundle:nil] autorelease];
self.viewController3 = [[[example3ViewController alloc] initWithNibName:#"example3ViewController_iPhone" bundle:nil] autorelease];
self.viewController4 = [[[example4ViewController alloc] initWithNibName:#"example4ViewController_iPhone" bundle:nil] autorelease];
}
self.rootController.viewControllers = [NSArray arrayWithObjects:self.viewController, self.viewController4, self.viewController1, self.viewController3, self.viewController2, nil];
[viewController release];
[viewController1 release];
[viewController2 release];
[viewController3 release];
[viewController4 release];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
self.window.rootViewController = self.rootController;
#else
[self.window addSubview:rootController.view];
#endif
[self.window makeKeyAndVisible];
HUD = [[MBProgressHUD alloc] initWithView:viewController.view];
[viewController.view addSubview:HUD];
[HUD show:NO];
HUD.delegate = self;
HUD.labelText = #"Loading";
return YES;
}
//[---CODE CLIP---]
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation {
CGFloat width = self.rootController.view.bounds.size.width*2;
CGFloat height = self.rootController.view.bounds.size.height;
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)];
UIImage *imageView = [UIImage imageNamed:#"TabBarBackground.png"];
UIColor *kMainColor = [[UIColor alloc] initWithPatternImage:imageView];
[v setBackgroundColor:kMainColor];
[kMainColor release];
[self.rootController.tabBar insertSubview:v atIndex:0];
[imageView release];
[v release];
}
- (void)hudWasHidden {
[HUD removeFromSuperview];
}
//[---CODE CLIP---]
- (void)dealloc
{
[_window release];
[rootController release];
[HUD release];
[super dealloc];
}
#end
The problem is that when I rotate the device in iOS Simulator, the application won't rotate. Any ideas would be much appreciated!
UPDATE
I have also noticed that the launch image is also not rotating (for iPad that is - iPhone doesn't do landscape launch images).
NOTE FOR JMANS
I overrode UITabBarController:
#implementation UITabBarController (MyApp)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if(self.selectedIndex==4)
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
else
return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
I would start by including this in all of your view controllers:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
Also, make sure that you are supporting multiple orientations in your Info.plist.
Tab Bar Controllers and View Rotation
Tab bar controllers support a portrait orientation by default and do not rotate to a landscape orientation unless all of the contained view controllers support such an orientation. When a device orientation change occurs, the tab bar controller queries its array of view controllers. If any one of them does not support the orientation, the tab bar controller does not change its orientation.
How can I have a UIView over a view that contains a live view from the camera?
Put a NavigationController in uour MainWindow.xib, with a first Controller pointing to a custom CameraController (class attribute). Don't specify any XIB.
Keep a reference to this navigationController with an IBOutlet into your appDelegate, then call at laucnch :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Create another xib for the overlay, with its controller, let's call it OverlayViewController.
Then into this CameraViewController :
.h
#interface CameraController : UIViewController {
UIImagePickerController* __picker;
OverlayViewController* __overlay;
}
#property (nonatomic, retain) UIImagePickerController* picker;
#property (nonatomic, retain) OverlayViewController* overlay;
.m
- (void) viewDidLoad {
self.picker = [[UIImagePickerController alloc] init];
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
self.picker.showsCameraControls = NO;
self.picker.navigationBarHidden = YES;
self.picker.wantsFullScreenLayout = YES;
// Insert the overlay
self.overlay = [[OverlayViewController alloc] initWithNibName:#"Overlay" bundle:nil];
self.overlay.pickerRef = self.picker;
self.picker.cameraOverlayView = self.overlay.view;
[self presentModalViewController:self.picker animated:NO];
}
This is a very important auto rotate issue and easy to reproduce.
My application has a UITabBarController. Each tab is a UINavigationController. Auto rotation is handled with normal calls to shouldAutorotateToInterfaceOrientation and didRotateFromInterfaceOrientation.
The interface rotates normally until I call UIViewController.popViewControllerAnimated and change UITabBarController.selectedIndex.
Steps to reproduce:
Create a demo Tab Bar Application.
Add the following code to the App Delegate .h file:#import <UIKit/UIKit.h>
#interface TestRotationAppDelegate : NSObject {
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
// Redefine the interface to cach rotation messages
#interface UITabBarController (TestRotation1AppDelegate)
#end
Add the following code to the App Delegate .m file:#import "TestRotationAppDelegate.h"
#implementation TestRotationAppDelegate
#synthesize window;
#synthesize tabBarController;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
#end
#implementation UITabBarController (TestRotation1AppDelegate)
-(void)viewDidLoad {
[super viewDidLoad];
// Add a third tab and push a view
UIViewController *view1 = [[[UIViewController alloc] init] autorelease];
view1.title = #"Third";
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:view1] autorelease];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObjectsFromArray:self.viewControllers];
[array addObject:nav];
self.viewControllers = array;
// Push view2 inside the third tab
UIViewController *view2 = [[[UIViewController alloc] init] autorelease];
[nav pushViewController:view2 animated:YES];
// Create a button to pop view2
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(50, 50, 220, 38);
[button setTitle:#"Pop this view" forState:UIControlStateNormal];
[button addTarget:self action:#selector(doAction) forControlEvents:UIControlEventTouchUpInside];
[view2.view addSubview:button];
}
-(void) doAction {
// ROTATION PROBLEM BEGINS HERE
// Remove one line of code and the problem doesn't occur.
[self.selectedViewController popViewControllerAnimated:YES];
self.selectedIndex = 0;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#end
The interface auto rotates normally until you tap the button on tab #3.
Your help will be geatly appreciated!
iPhone SDK 3.2 solves this issue.
With previous SDK use [self.selectedViewController popViewControllerAnimated:NO].
I'm new to iPhone programming, and I'm trying to make a simple program without a NIB. I have worked through some NIB tutorials, but I'd like to try some things programmatically.
My code loads without errors, makes the status bar black, and makes the background white. But, I don't think I'm loading my view with a label correctly after that. I presume I'm doing something just fundamentally wrong, so if you could point me in the right direction, I'd appreciate it. I think if I can get the label to show, I'll get some understanding. Here's my code:
//helloUAppDelegate.h
#import <UIKit/UIKit.h>
#import "LocalViewController.h"
#interface helloUAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
LocalViewController *localViewController;
}
#property (nonatomic, retain) UIWindow *window;
#property (nonatomic, retain) LocalViewController *localViewController;
#end
//helloUApDelegate.m
#import "helloUAppDelegate.h"
#implementation helloUAppDelegate
#synthesize window, localViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
application.statusBarStyle = UIStatusBarStyleBlackOpaque;
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if (!window) {
[self release];
return;
}
window.backgroundColor = [UIColor whiteColor];
localViewController = [[LocalViewController alloc] init];
[window addSubview:localViewController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
//LocalViewController.h
#import <UIKit/UIKit.h>
#interface LocalViewController : UIViewController {
UILabel *myLabel;
}
#property (nonatomic, retain) UILabel *myLabel;
#end
//LocalViewController.m
#import "LocalViewController.h"
#implementation LocalViewController
#synthesize myLabel;
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
self.myLabel.text = #"Lorem...";
self.myLabel.textColor = [UIColor redColor];
}
- (void)dealloc {
[super dealloc];
[myLabel release];
}
Add your label to your LocalViewController's view:
- (void)loadView {
[super loadView];
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
self.myLabel.text = #"Lorem...";
self.myLabel.textColor = [UIColor redColor];
[self addSubview:self.myLabel];
[self.myLabel release]; // since it's retained after being added to the view
}