disable button from another view controller in xcode - ios

I have a button that is initialized dynamically.. I want to disable that button from another UIViewController.
I am using this code:
The button is
#property (strong, nonatomic) IBOutlet UIButton *aboutus;
and then i try to disable it this way:
OtherViewController * view2 = [[OtherViewController alloc] initWithNibName:#"view2" bundle:nil];
view2.aboutus.enabled=NO;
but the button would still be enabled.. any ideas why?

You need a public BOOL property:
#property(nonatomic) buttonEnabled;
on OtherViewController. Set that value to whatever you want (YES or NO) and then in viewDidLoad:
aboutus.enabled = buttonEnabled;

Related

How to change container in another VC ? iOS

In ViewController i have two property of containers:
#property (weak, nonatomic) IBOutlet UIView *firstContainerView;
#property (weak, nonatomic) IBOutlet UIView *secondContainerView;
And i can do everything with them:
self.firstContainerView.alpha = 0;
self.secondContainerView.alpha = 1;
But how i can appear firstContainerView from SecondVC method (click button) ?
First of all you must have a pointer to access the target view controller so you see two vc not have a connection so you should create a global pointer for another vc to access. You can create a singleton to hold these vc or just use a delegate or block.
Why don't use a delegate to inform ViewController that button is clicked and ViewController will take care of appearing/disappearing the containers views.

iOS Container View and Parent ViewController communication?

How can I communicate with container view controller and parent view controller? I've tried to make a communication and pass the value between them. I've added a container view in the green view controller. And I had set segue name with "communicateSegue". I've searched some methods, I know we can set the prepareForSegue to pass the value at initial.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"communicateSegue"]) {
NSLog(#"call prepare for segue at parent view controller");
}
}
But now I want to pass value and change the label in the different viewcontroller . Not at the initial.
I don't know how to do?
Question:
Have anyone know how to implement ParentViewController pass value to Container View Controller and show the label in the ContainerViewController?
And reverse , Container View Controller click the button then pass the value and change the label show in the ParentViewController with objective-c?
Thanks.
My declare variable in parentViewController.h:
#import
#interface ParentViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *parentTF;
#property (weak, nonatomic) IBOutlet UIButton *passValToChildVCBtn;
#property (weak, nonatomic) IBOutlet UILabel *showContainerValLB;
#property (weak, nonatomic) IBOutlet IBAction *passValueToContainerEvent;
#end
In the ContainerChildViewController.h
#import
#interface ContainerChildViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *showParentValLB;
#property (weak, nonatomic) IBOutlet UITextField *childTF;
#property (weak, nonatomic) IBOutlet UIButton *passToParentBtn;
#property (weak, nonatomic) IBOutlet IBAction *passValueToParentBtnEvent;
#end
test project in the here.
thank you very much.
Regarding passing value to the containerView controller, you can make a property in the ChildViewController of the value you want it to be passed. Then in your ParentViewController do something like the following:
self.ChildViewController.yourProperty = yourValue
The opposite can be done in 4 ways:
You can make a delegate protocol to communicate the data between your controllers.
You can post a notification in your ChildViewController and add the parent controller as an observer.
You can use KVO.
And the easiest way out, you can make a property in your parentviewController and access it like the following
((YourParentViewControllerClassType *)self.parentViewController).yourParentProperty = TheValueYouWant;
I do it like this:
set identifier for segue from your containerView to viewController
make property/variable myChildViewController in your parent view controller and property/variable myParentViewController in your child view controller
in your prepareForSegue method set this properties to be the reference to right objects
Now you can access to properties of parent/child view controller through this references.
Example in Swift:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "YourIdentifier" {
// Set references
myChildViewController = segue.destinationViewController as! YourChildViewController
myChildViewController.myParentViewController = self
}
}
Example in Objective C
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"YourIdentifier"]) {
// Set references
myChildViewController = [segue destinationViewController];
myChildViewController.myParentViewController = self
}
}
I did't try the objective C code becouse I don't ever use it. It is just an example to get the idea.
After that you can set any property of parent from child view controller as:
myParentViewController.property = value
and in your parent view controller:
myChildViewController.property = value
Hope this helps anyone!
Is pretty simple, the UIStoryboardSegue contains a reference to your destination view controller using the property -destinationViewController.
In the method -prepareForSegue you can access it and pass the data that you need.
If you need to pass data from child to parent, I strongly suggest you to avoid hard coupling and use something such as delegation or notification pattern.
Remember that your parent view controller already contains a reference to you child view controllers and you can simply access it by using -childViewControllers property.
you can use NSNotificationCenter for updating all screens or you can do this way..
create a property in ContainerChildViewController.h
#property(nonatomic,copy)NSString * mytextString;
this property will be accessible in your ParentViewController.h if you import ContainerChildViewController.h
you have to create a NSString in ParentViewController.h
in this you can set button action to copy textfielddata.text
then you can assign this value to your ContainerChildViewcontroller's
as
// create an instance of 2nd view controller as second in ParenViewController
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ContainerChildViewController * second= [mystoryboard instantiateViewControllerWithIdentifier:#"ContainerChildViewController"];
second.mytextString = self.mytextString;
// either you directly pass this value or use the string it should work both ways
NSLog(#"%#",second.orderID);
[self.navigationController pushViewController:second animated:YES];
Reply if it works
try this
- (IBAction)showDetail:(UIButton *)sender {
DetailViewController *detailVc = [self.childViewControllers firstObject];
detailVc.lable.text = sender.titleLabel.text;
}

Why the view doesn't show when I click the button in IOS?

I am new to IOS , and I want to implement the album by collectionView in IOS.
When I click the filebrowserbutton button in first View , it will change to the second View(name:AITCameraCollectionBrowser).
There has two button and a collectionView in AITCameraCollectionBrowser like the following picture.
And the code of it's header file is like the following code.
#import <UIKit/UIKit.h>
#interface AITCameraCollectionBrowser : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource>
#property (strong, nonatomic) IBOutlet UIButton *backButton;
#property (strong, nonatomic) IBOutlet UIButton *localAlbumButton;
#property (strong, nonatomic) IBOutlet UICollectionView *collectionview;
#end
I have not load the picture from phone yet , so it should show the view like the above picture when I click the change view button in first view.
The code of filebrowserbutton button is like the following.
- (IBAction)filebrowserbutton:(id)sender {
UIViewController *viewController = nil ;
viewController = [[AITCameraCollectionBrowser alloc] initWithNibName:#"AITCameraCollectionBrowser" bundle:nil] ;
viewController.edgesForExtendedLayout = UIRectEdgeNone;
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Back", nil) style: UIBarButtonItemStyleBordered target:nil action:nil] ;
[self.navigationController pushViewController:viewController animated:YES];
}
The above code of button in first view is no problem.
But it doesn't show anything when I click the filebrowserbutton button , it only has a backBarButtonItem on the top of screen.
So the problem is at second view , and I don't know why the button and collectionView is not show ?
In you're header, or .h file, add the following code
- (IBAction)filebrowserbutton:(id)sender;
next, in you're xib or storyboard file, you have to connect filebrowserbutton from you're file's owner (by right-clicking the yellow, transparent box on the left of you're first picture) to the button, and choose touch up inside.
You need to add - (IBAction)filebrowserbutton:(id)sender; to your header file and connect it to your button's touchDown method.

How to add subview to another viewcontroller?

I'm doing an Ipad app. Now I have 2 viewcontrollers, ViewController has a button1 which has a popover segue to the second viewcontroller(PopoverController). Then, the PopoverController has a button2, if I click the button2, I'll receive some UIImage from my server. I want to add fews subviews of UIImageView to the ViewController to display these images if I click the button2.
The button1 works well, the PopoverController can pop up as expected. BUT when I click the button2, nothing happend. I want to know how can I pass the data between 2 viewcontrollers and how to add subviews to another one.
Some codes relating to my problem:
ViewController.h:
#import <UIKit/UIKit.h>
#class PopoverController;
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIButton *button1;
#property (strong, nonatomic) PopoverController *popoverController;
#end
PopoverController.h:
#import <UIKit/UIKit.h>
#class ViewController;
#interface PopoverController : UIViewController
#property (strong, nonatomic) IBOutlet UIButton *button2;
#property (strong, nonatomic) UIImage *tempImg;
#property (strong, nonatomic) ViewController *viewController;
- (IBAction)addsubviews:(id)sender;
#end
I can not just use [viewController.view addSubview:img1]; in the - (IBAction)addsubviews:(id)sender;method to addsubview. So someone can help me? :)
====1st update====
Someone suggest that I have to use - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender method. I have tried this one by control click the button2 and create a custom segue between button2 and ViewController. When I clicked the button2, it showed :
Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'change'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
So I'm wondering whether I should add a NavigationController. If so, what should I do?
====2nd update====
I use Paramasivan 's code now, and I found the way to call method from another viewcontroller. The problem now is the newly added subview in my viewcontroller doesn't show up. I guess I have to update my viewcontroller in order to make it visible.
in my - (IBAction)addsubviews:(id)sender; method, i invoke the method in ViewController by [self.viewController createSubViewWithImage:_tempImg];
so the method can be invoked when i click the button2, but the view of viewcontroller has nothing changed.
Add this in - (void)viewDidLoad,
self.popoverController = [[PopoverController alloc] init];
self.popoverController.viewController = self;
Make sure that in no other places, you are setting self.popoverController = ....
Do NOT add anything like self.viewController = ... in popovercontroller class. And you dont have to do self.viewController.popoverController = self; as well. Just remove these lines if you already have it.
Once these are done, make sure that you are displaying self.popoverController only in the popover and you are not creating a new object for popoverController class there. So if these are fine, you can use any approach you want for passing the image from popoverController class to viewController class.
as you mentioned in your comment you can use [self.viewController createSubViewWithImage:_tempImg]; in your popovercontroller class.
Update:
If you are doing via storyboard, you need to set this in prepareForSegue method and you dont have to create self.popoverController at all. Remove that part in your case. You can follow the procedure mentioned here to set up a custom segue and implement prepareForSegue method to pass the object. Source: On storyboards, views and passing data along
Set the name of segue in storyboard to "CustomSegue"
Implement prepareForSegue method
Inside the method, check if name of segue matches "CustomSegue" and then set the viewController in the popoverController object there as,
Try,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"CustomSegue"]) {
PopoverController *popoverController = [segue destinationViewController];
popoverController.viewController = self;
}
}
After doing this, you need to call [self.viewController createSubViewWithImage:_tempImg]; in your popoverController class.
Check out the Communicating with Objects doc, there are several ways to do what you want.
In ViewController.h add the following
-(void)createSubViewWithImage:(UIImage *)imageDownloaded {
UIImageView *imageViewTemp = [[UIImageView alloc] initWithImage:imageDownloaded];
[self.view addSubView:imageViewTemp];
}
In PopoverController.h add the following
#property (nonatomic, retain) ViewController *viewControllerPassed;
And after image downloaded, call the following in PopoverController.h
[viewControllerPassed createSubViewWithImage:imageDownloaded];
You can pass data using the below method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ViewController2"])
{
ViewController2 *v2VC = [segue destinationViewController];
v2VC.yourData = self.someStuff;
}
}

How to add a NavigationBarItem and UIBarButtonItem programmatically?

I presented a View using a call to presentModalViewController.
My new View didn't have a navigation bar, so I added one from the Library but now when I created a UIBarButtonItem in viewDidLoad of my controller, and set it to rightBarButtonItem of the navigationBarItem.
But when I run my app, the Navigation bar is there but there is no button.
Did I miss a step or something ?
Just adding a navigation bar manually will not make the self.navigationController property actual and it's value is nil, which, I believe, you're using for adding a UIBarButtonItem instance.
What I'd recommend is to declare a UINavigationBar IBOutlet in your controller, connect it in NIB editor with navigation bar you've added and after that in viewDidLoad use something like:
#interface ...
#property (nonatomic, retain) IBOutlet UINavigationBar *theBar;
#end
#implementation ...
#synthesize theBar;
-(void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *yourButton = ...;
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"MyTitle"];
item.rightBarButtonItem = yourButton;
[theBar pushNavigationItem: item animated:NO];
[item release];
...
}
#end
I thought
navigationBarItem property of UIViewController is work only if your ViewController is present with UINavigationController
if you add a navigation bar with interface builder
or add it with code ,then you need to use
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated
to add UINavigationItem yourself
you can create a UINavigationController
use - (id)initWithRootViewController:(UIViewController *)yourViewController
and use presentModalViewController to show UINavigationController that you create
then you can use navigationBarItem property of UIViewController to set rightBarButtonItem in viewDidLoad
Actually it is easier than this:
Drag a UINavigationBar into your app. Note that you will also get a UINavigationItem when you do this... make sure you keep it.
Add the following property to your app:
#property (weak, nonatomic) IBOutlet UINavigationItem *navigationItem;
Xcode will likely indicate an error (you are overriding a built in property of UIViewController), so you have to manually synthesize the getter and setter. Add the following line below the #implementation statement for the class:
#synthesize navigationItem;
In Interface Builder, drag a connection from the new navigationItem property to your custom navigation item.
You can now access your navigation item using self.navigationItem, just like you would normally.

Resources