customizing the uitabbaritem - objective-c

Hello I have been trying to customize the UItabBarItem and i have found some piece of code can customize the UItabbaritem but they aren't just working me. Nothing happens when i use any of the two code. Any help will be appreciated. thx in advance
[yourTabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[NSValue valueWithUIOffset:UIOffsetMake(0,0)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Helvetica" size:18.0], UITextAttributeFont, nil]
forState:UIControlStateNormal];
I have tried this one too, but this one isn't working either.
[[UITabBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
[UIColor whiteColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Rok" size:0.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
Here is my code
ViewController.h
// ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
UIImage *img;
UITabBarItem *tabBarItem;
}
#end
My ViewController.m
// ViewController.m
#import "ViewController.h"
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor purpleColor]];
img=[UIImage imageNamed:#"image2.jpg"];
//[[UITabBarItem appearance] setTintColor:[UIColor yellowColor]];
// [tabBarItem setTitleTextAttributes:<#(NSDictionary *)#> forState:<#(UIControlState)#>
// tabBarItem=[[UITabBarItem alloc]initWithTitle:#"View1" image:img tag:0];
[[UITabBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
[UIColor whiteColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Rok" size:0.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
self.tabBarItem=tabBarItem;
}
#end
And my AppDelegate.h
// AppDelegate.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
#import "ViewController1.h"
#import "ViewController2.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UITabBarController *tabBarController;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *vc;
#property (strong, nonatomic) ViewController1 *vc1;
#property (strong, nonatomic) ViewController2 *vc2;
#end
AppDelegate.m
// AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize window = _window,vc,vc1,vc2;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
tabBarController=[[UITabBarController alloc]init];
vc=[[ViewController alloc]init];
vc1=[[ViewController1 alloc]init];
vc2=[[ViewController2 alloc]init];
// CGRect frame = CGRectMake(0.0, 0.0, 32, 48);
// UIView *v=[[UIView alloc]initWithFrame:frame];
// v.backgroundColor=[UIColor redColor];
// tabBarController.view.backgroundColor=[UIColor redColor];
// [tabBarController adds
NSArray *controllersArray=[[NSArray alloc]initWithObjects:vc,vc1,vc2, nil];
// Override point for customization after application launch.
tabBarController.viewControllers=controllersArray;
tabBarController.selectedViewController=vc;
[self.window setRootViewController:tabBarController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#end
I have other ViewContoller also but for now my focus is one viewController , if i can customize the one then same i can do for others, for sake of saving save space and time. thx

UI element customization can be done using the tutorial

Related

Push to UIViewController from a UITabBarItem

I encounter a problem using UITabBarItem and UIButton in my application. My button is inside a UITabBarItem. When I press it I want to be pushed to another controller to display a PDF.
Here is a piece of code that works in other cases :
- (void)viewDidLoad {
UIImage* imageButton = [UIImage imageNamed:#"pdf-button.png"];
UIButton *buttonPDF = [UIButton buttonWithType:UIButtonTypeCustom];
buttonPDF.frame = CGRectMake(SCREEN_WIDTH/2 - 100, 100, 200, 36);
[buttonPDF setImage:imageButton forState:UIControlStateNormal];
buttonPDF.contentMode = UIViewContentModeScaleAspectFit;
buttonPDF.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
[buttonPDF addTarget:self action:#selector(displayPDFParams:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonPDF];
}
-(void)displayPDFParams:(UIButton *)sender {
PDFProduitController *pdfController = [[PDFProduitController alloc] init];
pdfController.pdf = documentParametres;
[self.navigationController pushViewController:pdfController animated:YES];
}
displayPDFParams is called but it not push me on my pdfController. I think it's because I'm not able to target the parent navigation controller of my application...
Anyone help would be much appreciated. Thanks in advance !
You need To initialize your root view controller with the navigation controller. Here is the code.
In your AppDelegate.h
#import <UIKit/UIKit.h>
#import "HomeViewController.h"
#class HomeViewController;
#interface IDSAppDelegate : UIResponder <UIApplicationDelegate>{
UINavigationController *nav;
}
#property (strong, nonatomic) HomeViewController *homeViewController;
#end
In your AppDelegate.m
#import "IDSAppDelegate.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.homeViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
nav=[[UINavigationController alloc]initWithRootViewController:self.homeViewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
Problem solved by define a property in my UIViewController (as UITabBarItem) like this :
#property (nonatomic, retain) UINavigationController *superNavController;
And set it in my UITabBarController :
self.myViewController.superNavController = self.navigationController;
Finally I modified my displayPDFParams method :
-(void)displayPDFParams:(UIButton *)sender {
PDFProduitController *pdfController = [[PDFProduitController alloc] init];
pdfController.pdf = self.documentParametres;
[self.superNavController pushViewController:pdfController animated:YES];
}
Works perfectly !

ios tabbar customization with images

i'm working in ios application i need to customize tabbar to be like this
First I created 5 viewcontrollers each one in navigation controller
then put them in tabbbarcontroller
I googled for this problem and I found solution
[self.tabBarItem setFinishedSelectedImage:<#(UIImage *)#> withFinishedUnselectedImage:<#(UIImage *)#>]
but it is for iOS 5, I need solution for both iOS 4 and iOS 5.
For customizing tab bar in ios4 is not available with code for that you need to make us custom tab bar for that you can refer this Que.
How to Customize the tabbarcontroller
or you also can do like simple logic with making full tab bar image like this
here i have made one image view on appdel did finish method and done like this in the app.
self.imgV.frame=CGRectMake(0, 431, 320, 49);
[self.tabbarcontroller.view addSubview:self.imgV];
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];
NSString *deviceType = [UIDevice currentDevice].model;
NSLog(#"Device%#",deviceType);
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad){
// self.imgV.frame=CGRectMake(0, 975, 768, 49);
//[self.tabbarcontroller.view addSubview:self.imgV];
switch (index) {
case 0:
self.imgV.image=[UIImage imageNamed:#"reservation_tab~iPad.png"];
break;
case 1:
self.imgV.image=[UIImage imageNamed:#"place_order_tab~iPad.png"];
break;
case 2:
self.imgV.image=[UIImage imageNamed:#"location_tab~iPad.png"];
break;
case 3:
self.imgV.image=[UIImage imageNamed:#"favorite_tab~iPad.png"];
break;
case 4:
self.imgV.image=[UIImage imageNamed:#"signature_dishes_tab~iPad.png"];
break;
case 5:
self.imgV.image=[UIImage imageNamed:#"history_tab~iPad.png"];
break;
case 6:
self.imgV.image=[UIImage imageNamed:#"contact_us_tab~iPad.png"];
break;
default:
break;
}
}
else{
switch (index) {
case 0:
self.imgV.image=[UIImage imageNamed:#"reservation_tab.png"];
break;
case 1:
self.imgV.image=[UIImage imageNamed:#"place_order_tab.png"];
break;
case 2:
self.imgV.image=[UIImage imageNamed:#"location_tab.png"];
break;
case 3:
self.imgV.image=[UIImage imageNamed:#"favorite_tab.png"];
break;
case 4:
self.imgV.image=[UIImage imageNamed:#"gallery_tab.png"];
break;
default:
break;
}
}
return YES;
}
In the AppDelegate.m file, add the following code. In that piece of code, we are creating four views and adding them to a Tab Controller. These views are empty for now because we don’t need any content in them for the purposes of this project.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = [[UIViewController alloc] init];
UIViewController *viewController2 = [[UIViewController alloc] init];
UIViewController *viewController3 = [[UIViewController alloc] init];
UIViewController *viewController4 = [[UIViewController alloc] init];
tabController.viewControllers = [NSArray arrayWithObjects:viewController1,
viewController2,
viewController3,
viewController4, nil];
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
return YES;
}
You can see a good tutorial here
try this
paste it .h file
#import <UIKit/UIKit.h>
#class MapViewController,MenuViewController;
#interface UITabBar (ColorExtensions)
- (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur;
#end
#interface UITabBarItem (Private)
#property(retain, nonatomic) UIImage *selectedImage;
- (void)_updateView;
#end
#interface SegmentedControlExampleAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow * window;
UINavigationController * navigationController;
NSMutableArray *breads;
NSMutableArray *categorys;
NSMutableArray *collections;
NSString *databaseName;
NSString *databasePath;
MapViewController *mapViewController;
MenuViewController *wvTutorial;
}
#property (nonatomic, retain) IBOutlet UIWindow * window;
#property (nonatomic, retain) UINavigationController * navigationController;
#property (nonatomic,retain) NSMutableArray *breads;
#property (nonatomic,retain) NSMutableArray *categorys;
#property (nonatomic,retain) NSMutableArray *collections;
#property (nonatomic, retain) UITabBarController *tabBarController;
#property (nonatomic, retain) MenuViewController *wvTutorial;
#end
In .m file
#import "SegmentedControlExampleAppDelegate.h"
#import "SegmentManagingViewController.h"
#import "sqlite3.h"
#import "AtoZHomePageViewController.h"
#import "CategoryViewHomePage.h"
#import "CollectionsListHomePageViewController.h"
#import "AboutUs.h"
#import "StoreLocatorViewController.h"
#import "UINavigationBar+CustomImage.h"
#import "MenuViewController.h"
#implementation UITabBar (ColorExtensions)
- (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur
{
CGColorRef cgColor = [color CGColor];
CGColorRef cgShadowColor = [shadowColor CGColor];
for (UITabBarItem *item in [self items])
if ([item respondsToSelector:#selector(selectedImage)] &&
[item respondsToSelector:#selector(setSelectedImage:)] &&
[item respondsToSelector:#selector(_updateView)])
{
CGRect contextRect;
contextRect.origin.x = 0.0f;
contextRect.origin.y = 0.0f;
contextRect.size = [[item selectedImage] size];
// Retrieve source image and begin image context
UIImage *itemImage = [item image];
CGSize itemImageSize = [itemImage size];
CGPoint itemImagePosition;
itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);
UIGraphicsBeginImageContext(contextRect.size);
CGContextRef c = UIGraphicsGetCurrentContext();
// Setup shadow
CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);
// Setup transparency layer and clip to mask
CGContextBeginTransparencyLayer(c, NULL);
CGContextScaleCTM(c, 1.0, -1.0);
CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]);
// Fill and end the transparency layer
CGContextSetFillColorWithColor(c, cgColor);
contextRect.size.height = -contextRect.size.height;
CGContextFillRect(c, contextRect);
CGContextEndTransparencyLayer(c);
// Set selected image and end context
[item setSelectedImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
// Update the view
[item _updateView];
}
}
#end
#implementation SegmentedControlExampleAppDelegate
#synthesize window,tabBarController, navigationController,breads,categorys,collections,wvTutorial;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
databaseName = #"ProductsConnect_Master.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
self.tabBarController = [[UITabBarController alloc] init];
UIViewController *viewController = [[AboutUs alloc] initWithNibName:#"AboutUs" bundle:nil];
UIViewController *viewController2 = [[StoreLocatorViewController alloc] initWithNibName:#"StoreLocatorViewController" bundle:nil];
//UIViewController *viewController3 = [[MenuViewController alloc] initWithNibName:#"MenuViewController" bundle:nil];
self.wvTutorial = [[MenuViewController alloc]initWithNibName:#"MenuViewController" bundle:nil];
SegmentManagingViewController * segmentManagingViewController = [[SegmentManagingViewController alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:segmentManagingViewController];
tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController,viewController ,viewController2,wvTutorial , nil];
[[UITabBar appearance]
setTintColor: [UIColor colorWithRed:120.0f/255.0f green:69.0f/255.0f blue:50.0f/255.0f alpha:1.0f]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor colorWithRed:255.0f/255.0f green:252.0f/255.0f blue:235.0f/255.0f alpha:1.0f]];
//[[UITabBar appearance]
// setBackgroundColor: [UIColor colorWithRed:255.0f/255.0f green:252.0f/255.0f blue:235.0f/255.0f alpha:0.8f]];
navigationController.title = NSLocalizedString(#"HomePage", #"HomePage");
navigationController.tabBarItem.image = [UIImage imageNamed:#"logoSmall"];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
UIImage *navImage = [UIImage imageNamed:#"logoSmall.png"];
// self.navigationItem.setImage: navImage;
[[navigationController navigationBar] performSelectorInBackground:#selector(setBackgroundImage:) withObject:navImage];
// UIImage *navImage = [UIImage imageNamed:#"logoSmall.png"];
//[[navigationController navigationBar] performSelectorInBackground:#selector(setBackgroundImage:) withObject:navImage];
[self.window addSubview:tabBarController.view];
[segmentManagingViewController release];
//[window addSubview:self.navigationController.view];
[window makeKeyAndVisible];
return YES;
}
i have used this code working fine for me.

Who's know why black area is occured in iOS Simulator Retina 4-inch?

When I test in iOS Simulator Retina 3.5-inch, it is well work.
but when in Retina 4-inch, it is occured black area like following a picture.
I can't know this reason...
Would you give me a some idea?
Following is AppDelegate.h & m
#import <UIKit/UIKit.h>
#import "MenuViewCon.h"
#class TestViewCon;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) TestViewCon *viewController;
#end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[[TestViewCon alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
} else {
self.viewController = [[[TestViewCon alloc] initWithNibName:#"ViewController_iPad" bundle:nil] autorelease];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Next is TestViewCon.h & m
#import <Foundation/Foundation.h>
#interface TestViewCon : UIViewController
#end
#import "TestViewCon.h"
#implementation TestViewCon {
}
- (void)viewDidLoad {
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)] autorelease];
label.text = #"Retina 4 inch testing";
[label sizeToFit];
[self.view addSubview:label];
}
#end
This is because you app runs letterboxed.
It seems, that you don't support the iphone 5 within your project.
To do that, you need to add a Default-568h#2x.png Image to your project and set it as the launchimage for the iPhone 5.

UILabel Text does not update

The main view has a label and a button. I added one subview which contain a UILabel and a button. What I want to do is that I want to update two labels at the both view by pressing one of two button.
The problem is, if I click the button of the subview, the label of the subview is only updated. Conversely, the label of the main view is only changed when I click the button in the main view.
I am wondering how to update each label.text of the other view.
Thanks in advance...
Here is my code.
1. ViewController.h
#import <UIKit/UIKit.h>
#import "SubView.h"
#interface ViewController : UIViewController{
SubView *subView;
}
#property (weak, nonatomic) SubView *subView;
#property (retain, nonatomic) IBOutlet UILabel *amount;
#end
2. ViewController.m
#import "ViewController.h"
#import "SubView.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize subView=_subView;
#synthesize amount;
- (void)viewDidLoad
{
[super viewDidLoad];
subView = [[SubView alloc]initWithFrame:CGRectMake(10, 10, 300, 600)];
[self.view addSubview:subView];
amount = [[UILabel alloc]initWithFrame:CGRectMake(400, 400, 100, 50)];
amount.text = #"test in the main view";
[self.view addSubview:amount];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(400, 300, 40, 20);
btn.backgroundColor = [UIColor clearColor];
[btn addTarget:self action:#selector(labelChange:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn]; // add a button in the main view
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)labelChange:(id)sender{
amount.text = #"defaul label";
SubView *sv = [[SubView alloc]init];
sv.addObject2.text = #"label in the subview";
NSLog(#"sv.addObject2.text = %#",sv.addObject2);
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
3. SubView.h
#import <UIKit/UIKit.h>
#interface SubView : UIView {
UIButton *btn;
UILabel *label;
}
//#property (strong, nonatomic) UIView *view;
#property (weak, nonatomic) UIButton *btn;
#property (weak, nonatomic) UILabel *label;
- (UIButton *)addObject1;
- (UILabel *)addObject2;
#end
4. SubView.m
#import "SubView.h"
#import "ViewController.h"
#implementation SubView
#synthesize btn=_btn, label=_label;
//#synthesize view;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
//UIView *contentView = [[UIView alloc]initWithFrame:frame];
self.backgroundColor = [UIColor blackColor];
UIButton *object1 = [self addObject1];
UILabel *object2 = [self addObject2];
[self addSubview:object1];
[self addSubview:object2];
return self;
}
- (UIButton *)addObject1{
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 10, 100, 20);
btn.backgroundColor = [UIColor clearColor];
[btn addTarget:self action:#selector(labelChange:) forControlEvents:UIControlEventTouchUpInside];
return btn;
}
- (IBAction)labelChange:(id)sender{
label.text = #"change the label in the subview";
ViewController *vc = [[ViewController alloc]init];
[vc.amount setText:#"change the label in the mainview"];
}
- (UILabel *)addObject2{
label = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 200, 50)];
[label setText: #"default"];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:#"Arial" size:12];
label.textColor = [UIColor whiteColor];
return label;
}
#end
I think u need to read "The Objective-C Programming Language" and know how the "Object" and "Pointer" means
Here is the code
SubView.h
#import <UIKit/UIKit.h>
#interface SubView : UIView {
UIButton *btn;
UILabel *label;
}
#property (weak, nonatomic) UIViewController *vc;
#property (weak, nonatomic) UIButton *btn;
#property (weak, nonatomic) UILabel *label;
- (UIButton *)addObject1;
- (UILabel *)addObject2;
#end
Subview.m
#import "SubView.h"
#import "ViewController.h"
#implementation SubView
#synthesize btn=_btn, label=_label;
#synthesize vc;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
self.backgroundColor = [UIColor blackColor];
UIButton *object1 = [self addObject1];
UILabel *object2 = [self addObject2];
[self addSubview:object1];
[self addSubview:object2];
return self;
}
- (UIButton *)addObject1{
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 10, 100, 20);
btn.backgroundColor = [UIColor clearColor];
[btn addTarget:self action:#selector(labelChange:) forControlEvents:UIControlEventTouchUpInside];
return btn;
}
- (IBAction)labelChange:(id)sender{
label.text = #"change the label in the subview";
[vc.amount setText:#"change the label in the mainview"];
}
- (UILabel *)addObject2{
label = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 200, 50)];
[label setText: #"default"];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:#"Arial" size:12];
label.textColor = [UIColor whiteColor];
return label;
}
#end
ViewController.m
#import "ViewController.h"
#import "SubView.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize subView=_subView;
#synthesize amount;
- (void)viewDidLoad
{
[super viewDidLoad];
subView = [[SubView alloc]initWithFrame:CGRectMake(10, 10, 300, 600)];
subView.vc = self;
[self.view addSubview:subView];
amount = [[UILabel alloc]initWithFrame:CGRectMake(400, 400, 100, 50)];
amount.text = #"test in the main view";
[self.view addSubview:amount];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(400, 300, 40, 20);
btn.backgroundColor = [UIColor clearColor];
[btn addTarget:self action:#selector(labelChange:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn]; // add a button in the main view
}
- (IBAction)labelChange:(id)sender{
amount.text = #"defaul label";
subview.addObject2.text = #"label in the subview";
}
#end
I think I got what you are trying to do and here is what you should do:
1- Create 3 new files (xib,m,h) call them SubView.xib,m,h. You have to make sure it inherits UIView.
2- in the xib file draw the subview elements and hook the IBOutlets with it. But make sure that the file owner is the ViewController and the root view class type is "SubView"
3- Make an IBOutlet of type Subview in ViewController
4- open SubView.xib and hook the SubView (the root node in the objects tree) with the File Owner IBOutlet you made # step 3
to load the new SubView in the ViewController.m simply call this line:
after that line your IBOutlet in ViewController will have the object of SubView.
[[NSBundle mainBundle] loadNibNamed:#"SubView" owner:self options:nil];
in the above code self is reflected to the ViewController

UIButton not working inside UIScrollView

I have a scrollview in which i am creating buttons programmatically. On touch, button is supposed to load another nib. The problem is, on touch, i am getting
[ViewController buttonTest]: unrecognized selector sent to instance
Let me take you through the code:
appdelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UINavigationController *viewController;
appdelegate.m
#import "ViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *view = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.viewController = [[UINavigationController alloc] initWithRootViewController:view];
self.window.rootViewController = self.viewController;
[application setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
[self.window makeKeyAndVisible];
return YES;
}
Viewcontroller.h
#class newView;
#interface ViewController : UIViewController{
UIScrollView * scrollView;
IBOutlet UIButton *btn;
}
- (IBAction)butttonTest:(id)sender;
#property (strong, nonatomic) newView *secondView;
and finally, ViewController.m
- (void)loadView {
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
scrollView.contentSize=CGSizeMake(320,960);
UIImageView *tempImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpeg"]];
UIImage * buttonImage = [UIImage imageNamed:#"contentlist_active.png"];
self.view=scrollView;
[scrollView addSubview:tempImageView2];
scrollView.userInteractionEnabled = YES;
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(22, 100, 277, 32);
[btn setBackgroundImage:buttonImage forState:UIControlStateNormal];
[btn setTitle:#"hello world" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonTest:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:btn];
}
To load the view, I have code:
[btn addTarget:self action:#selector(buttonTest) forControlEvents:UIControlEventTouchUpInside];
What am i doing wrong?
OH yes, here is the method for buttenPressed
- (IBAction)butttonTest:(id)sender {
self.secondView = [[newView alloc] initWithNibName:#"newView" bundle:nil];
[self.navigationController pushViewController:self.secondView animated:YES];
}
your method is called - (IBAction)butttonTest:(id)sender.
Three t and a parameter.
But your selector is #selector(buttonTest). Two t and no parameter.
The two method signatures just don't match.
change the addTarget:action:forControlEvents: call like this:
[btn addTarget:self action:#selector(butttonTest:) forControlEvents:UIControlEventTouchUpInside];
or remove the third t on both.
Have you implemented the buttonTest: method (as i dont see it in the code above)?
Try this:
[btn addTarget:self action:#selector(buttonTest:) forControlEvents:UIControlEventTouchUpInside];
Note buttonTest: rather than buttonTest.